从另一个类调用类方法

Lar*_*ark 39 python

在Python中,有没有办法从另一个类调用类方法?我试图在Python中使用自己的MVC框架,我无法弄清楚如何从另一个类中的一个类调用一个方法.

这就是我想要发生的事情:

class A:
    def method1(arg1, arg2):
        # do code here

class B:
    A.method1(1,2)
Run Code Online (Sandbox Code Playgroud)

我正在慢慢从PHP进入Python,所以我正在寻找Python的Python等价物call_user_func_array().

aar*_*ing 44

更新:刚看到call_user_func_array你帖子中的引用.那不一样.用于getattr获取函数对象,然后使用您的参数调用它

class A(object):
    def method1(self, a, b, c):
        # foo

method = A.method1
Run Code Online (Sandbox Code Playgroud)

method现在是一个实际的函数对象.你可以直接调用(函数是python中的第一类对象,就像在PHP> 5.3中一样).但是下面的考虑仍然适用.也就是说,除非你A.method1使用下面讨论的两个装饰器中的一个进行装饰,否则上面的例子将会爆炸,传递一个A作为第一个参数的实例或者在一个实例上访问该方法A.

a = A()
method = a.method1
method(1, 2)
Run Code Online (Sandbox Code Playgroud)

这样做有三种选择

  1. 使用一个实例A来调用method1(使用两种可能的形式)
  2. 在应用classmethod装饰器method1:您将不再能够引用selfmethod1,但你会得到通过了cls在它的位置是例如A在这种情况下.
  3. 在应用staticmethod装饰器method1:您将不再能够引用self,或clsstaticmethod1但你可以硬编码到引用A到它,但很明显,这些引用将所有的子类继承A,除非他们专门覆盖method1,不叫super.

一些例子:

class Test1(object): # always inherit from object in 2.x. it's called new-style classes. look it up
    def method1(self, a, b):
        return a + b

    @staticmethod
    def method2(a, b):
        return a + b

    @classmethod
    def method3(cls, a, b):
        return cls.method2(a, b)

t = Test1()  # same as doing it in another class

Test1.method1(t, 1, 2) #form one of calling a method on an instance
t.method1(1, 2)        # form two (the common one) essentially reduces to form one

Test1.method2(1, 2)  #the static method can be called with just arguments
t.method2(1, 2)      # on an instance or the class

Test1.method3(1, 2)  # ditto for the class method. It will have access to the class
t.method3(1, 2)      # that it's called on (the subclass if called on a subclass) 
                     # but will not have access to the instance it's called on 
                     # (if it is called on an instance)
Run Code Online (Sandbox Code Playgroud)

请注意,与self变量名称完全取决于您的方式相同,变量的名称也是如此,cls但这些是常规值.

现在,你知道怎么做了,我会认真思考一下,如果你想这样做.通常,被称为未绑定(没有实例)的方法最好留在python中作为模块级函数.


rat*_*ile 9

只需打电话给它 self

class A:
    def m(self, x, y):
        print(x+y)

class B:
    def call_a(self):
        A.m(self, 1, 2)

b = B()
b.call_a()
Run Code Online (Sandbox Code Playgroud)

输出:3

  • 这个例子是错误的,因为你将 B 类引用传递给 A 类方法 (3认同)
  • @VarunMaurya,Python 使用鸭子类型,因此不会检查类。正如 a1an 所说,只要您提供具有正确属性的对象,这就会起作用。 (3认同)

Xrh*_*oui 7

class CurrentValue:

    def __init__(self, value):
        self.value = value

    def set_val(self, k):
        self.value = k

    def get_val(self):
        return self.value


class AddValue:

    def av(self, ocv):
        print('Before:', ocv.get_val())
        num = int(input('Enter number to add : '))
        nnum = num + ocv.get_val()
        ocv.set_val(nnum)
        print('After add :', ocv.get_val())


cvo = CurrentValue(5)

avo = AddValue()

avo.av(cvo)
Run Code Online (Sandbox Code Playgroud)

我们定义了 2 个类,CurrentValue 和 AddValue 我们在第一个类中定义了 3 个方法一个init为了给实例变量 self.value 一个初始值一个 set_val 方法,我们将 self.value 设置为 ak 一个 get_val 方法,我们得到self.value 的值我们在第二个类中定义一个方法 av 方法,我们将第一个类的对象作为参数(ovc)传递我们创建第一个类的实例(cvo)我们创建一个实例(avo)第二个类我们调用第二个类的 avo.av(cvo) 方法并将我们已经从第一个类创建的对象作为参数传递。所以通过这种方式,我想展示如何从另一个类调用一个类的方法。

对于任何不便,我深表歉意。这不会再发生了。

之前:5

输入要添加的数字:14

添加后:19

  • 您在“ocv.get_val()”上有拼写错误。应该是cvo而不是ocv吗? (2认同)