从另一个类调用类方法

时间:2021-06-18 04:03:25

In Python, is there a way to call a class method from another class? I am attempting to spin my own MVC framework in Python and I can not figure out how to invoke a method from one class in another class.

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

Here is what I want to happen:

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

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

class B:
    A.method1(1,2)

I am slowly getting into Python from PHP so I am looking for the Python equivalent of PHP's call_user_func_array().

我正在慢慢从PHP进入Python,所以我正在寻找Python的call_user_func_array()等效的Python。

3 个解决方案

#1


37  

update: Just saw the reference to call_user_func_array in your post. that's different. use getattr to get the function object and then call it with your arguments

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

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

methodname = 'method1'
method = getattr(A, methodname)

method is now an actual function object. that you can call directly (functions are first class objects in python just like in PHP > 5.3) . But the considerations from below still apply. That is, the above example will blow up unless you decorate A.method1 with one of the two decorators discussed below, pass it an instance of A as the first argument or apply getattr to an instance of A.

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

a = A()
method = getattr(a, methodname)
method(1, 2)

You have three options for doing this

这样做有三种选择

  1. Use an instance of A to call method1 (using two possible forms)
  2. 使用A的实例来调用method1(使用两种可能的形式)

  3. apply the classmethod decorator to method1: you will no longer be able to reference self in method1 but you will get passed a cls instance in it's place which is A in this case.
  4. 将classmethod装饰器应用于method1:你将不再能够在方法1中引用self,但是在这种情况下你将在它的位置传递一个cls实例。

  5. apply the staticmethod decorator to method1: you will no longer be able to reference self, or cls in staticmethod1 but you can hardcode references to A into it, though obviously, these references will be inherited by all subclasses of A unless they specifically override method1 and do not call super.
  6. 将staticmethod装饰器应用于method1:你将不再能够在staticmethod1中引用self或cls,但你可以将对A的引用硬编码到其中,尽管很明显,这些引用将由A的所有子类继承,除非它们专门覆盖method1和不要叫超级。

Some examples:

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)

Note that in the same way that the name of the self variable is entirely up to you, so is the name of the cls variable but those are the customary values.

请注意,与自变量的名称完全取决于您的方式相同,cls变量的名称也是如此,但这些是习惯值。

Now that you know how to do it, I would seriously think about if you want to do it. Often times, methods that are meant to be called unbound (without an instance) are better left as module level functions in python.

既然你知道怎么做,我会认真思考你是否想要这样做。通常,被称为未绑定(没有实例)的方法最好留在python中作为模块级函数。

#2


6  

Just call it and supply 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()

output: 3

#3


0  

You can call a function from within a class with:

您可以在类中调用函数:

A().method1()

#1


37  

update: Just saw the reference to call_user_func_array in your post. that's different. use getattr to get the function object and then call it with your arguments

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

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

methodname = 'method1'
method = getattr(A, methodname)

method is now an actual function object. that you can call directly (functions are first class objects in python just like in PHP > 5.3) . But the considerations from below still apply. That is, the above example will blow up unless you decorate A.method1 with one of the two decorators discussed below, pass it an instance of A as the first argument or apply getattr to an instance of A.

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

a = A()
method = getattr(a, methodname)
method(1, 2)

You have three options for doing this

这样做有三种选择

  1. Use an instance of A to call method1 (using two possible forms)
  2. 使用A的实例来调用method1(使用两种可能的形式)

  3. apply the classmethod decorator to method1: you will no longer be able to reference self in method1 but you will get passed a cls instance in it's place which is A in this case.
  4. 将classmethod装饰器应用于method1:你将不再能够在方法1中引用self,但是在这种情况下你将在它的位置传递一个cls实例。

  5. apply the staticmethod decorator to method1: you will no longer be able to reference self, or cls in staticmethod1 but you can hardcode references to A into it, though obviously, these references will be inherited by all subclasses of A unless they specifically override method1 and do not call super.
  6. 将staticmethod装饰器应用于method1:你将不再能够在staticmethod1中引用self或cls,但你可以将对A的引用硬编码到其中,尽管很明显,这些引用将由A的所有子类继承,除非它们专门覆盖method1和不要叫超级。

Some examples:

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)

Note that in the same way that the name of the self variable is entirely up to you, so is the name of the cls variable but those are the customary values.

请注意,与自变量的名称完全取决于您的方式相同,cls变量的名称也是如此,但这些是习惯值。

Now that you know how to do it, I would seriously think about if you want to do it. Often times, methods that are meant to be called unbound (without an instance) are better left as module level functions in python.

既然你知道怎么做,我会认真思考你是否想要这样做。通常,被称为未绑定(没有实例)的方法最好留在python中作为模块级函数。

#2


6  

Just call it and supply 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()

output: 3

#3


0  

You can call a function from within a class with:

您可以在类中调用函数:

A().method1()