从另一个类调用类方法 [英] Call Class Method From Another Class

查看:43
本文介绍了从另一个类调用类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,有没有办法从另一个类调用一个类方法?我试图在 Python 中使用我自己的 MVC 框架,但我不知道如何从另一个类中的一个类调用一个方法.

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.

这是我想要发生的事情:

Here is what I want to happen:

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

class B:
    A.method1(1,2)

我正在慢慢地从 PHP 开始使用 Python,所以我正在寻找与 PHP 的 call_user_func_array() 等效的 Python.

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

推荐答案

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

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

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

method = A.method1

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

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 access the method on an instance of A.

a = A()
method = a.method1
method(1, 2)

<小时>

您有三种选择来执行此操作


You have three options for doing this

  1. 使用A的实例调用method1(使用两种可能的形式)
  2. classmethod 装饰器应用于 method1:您将无法再在 method1 中引用 self,但是您将在它的位置获得一个 cls 实例,在本例中为 A.
  3. staticmethod 装饰器应用于 method1:您将无法再引用 selfclsstaticmethod1 中,但您可以将 A 的引用硬编码到其中,尽管很明显,这些引用将被 A 的所有子类继承,除非它们明确覆盖method1 并且不要调用 super.
  1. Use an instance of A to call method1 (using two possible forms)
  2. 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.
  3. 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.

一些例子:

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)

请注意,self 变量的名称完全取决于您,cls 变量的名称也是如此,但这些都是惯用值.

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.

既然你知道怎么做,我会认真考虑如果你想这样做.很多时候,打算被称为未绑定(没有实例)的方法最好保留为 Python 中的模块级函数.

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.

这篇关于从另一个类调用类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆