从另一个类调用类方法

84 投票
6 回答
442780 浏览
提问于 2025-04-16 04:58

有没有办法从一个类里调用另一个类的方法?我想要的功能有点像PHP里的 call_user_func_array()。我想要实现的效果是:

class A:
    def method1(arg1, arg2):
        ...

class B:
    A.method1(1, 2)

6 个回答

6
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)

我们定义了两个类,分别叫做 CurrentValue 和 AddValue。
在第一个类里,我们定义了三个方法:
1. 一个叫init的方法,用来给实例变量 self.value 设置一个初始值。
2. 一个叫 set_val 的方法,用来把 self.value 设置成一个值 k。
3. 一个叫 get_val 的方法,用来获取 self.value 的值。
在第二个类里,我们定义了一个方法:
一个叫 av 的方法,它的参数是一个来自第一个类的对象(ovc)。
我们创建了第一个类的一个实例(cvo)。
然后,我们又创建了第二个类的一个实例(avo)。
接着,我们调用第二个类的 av 方法,传入之前创建的第一个类的对象 cvo。
通过这种方式,我想展示如何从一个类中调用另一个类的方法。

抱歉给您带来不便。
这种情况不会再发生了。

之前的值:5

输入要添加的数字:14

添加后的值:19

20

只需要调用它,并提供 self

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

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

b = B()
b.call_a()

输出结果:

3
78

更新:我刚看到你帖子里提到的 call_user_func_array,那是不同的。你可以用 getattr 来获取函数对象,然后用你的参数来调用它。

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

method = A.method1

现在 method 是一个真正的函数对象,你可以直接调用它(在 Python 中,函数和 PHP 5.3 以上版本一样,都是一等公民)。不过,下面提到的注意事项仍然适用。也就是说,以上的例子会出错,除非你给 A.method1 加上下面讨论的两个装饰器之一,或者把 A 的实例作为第一个参数传进去,或者在 A 的实例上调用这个方法。

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

你有三种选择来做到这一点:

  1. 使用 A 的实例来调用 method1(有两种可能的形式)
  2. method1 加上 classmethod 装饰器:这样你就不能在 method1 中引用 self 了,但你会得到一个 cls 实例,代表的是 A
  3. method1 加上 staticmethod 装饰器:这样你就不能在 staticmethod1 中引用 selfcls 了,但你可以把 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)

请注意,self 这个变量的名字完全由你决定,cls 也是,但这些是习惯用法。

现在你知道怎么做了,我建议你认真考虑一下 是否 真想这么做。很多时候,设计成不绑定(没有实例)调用的方法,最好还是作为模块级别的函数来处理。

撰写回答