2016-03-05 78 views
-4

嘿,伙计们需要知道的如何在从类B中的ClassA的启动方法如何从python中的不同类启动或调用函数?

classA(object): 
     def __init__(self): 
#this is where the ClassB method'' def multiplyPeople() ''should be called or started. 

classB(object): 
    def multiplyPeople(self): 

它给了一个错误

TypeError: unbound method multiplyPeople() must be called 
with classB instance as first argument (got nothing instead)  

知道这是一些基本的东西,但我试图弄清楚究竟应该做什么以及我迷失方向。

我把它称为是

classA(object): 

def__init__(self): 
self.PeopleVariable=classB.multiplyPeople() 
+0

你是怎样尝试调用呢? –

+2

你能展示你的更多代码吗?使用您提供的代码,它与您提供的错误消息无关。此外,您提供的代码功能不完整,因为它有语法错误。请发布您的实际代码 – idjaw

+0

您需要先创建一个“B”实例,然后才能调用此类的方法(除非您使用的是静态或类方法) – MSeifert

回答

0

这取决于你如何想要的功能工作。你只是想用这个类作为占位符吗?然后你可以使用一个所谓的静态方法,你不需要实例化一个对象。

或者你也可以使用常规的方法,并使用它创建的对象上(注意,有你有机会获得self

class A(): 
    def __init__(self): 
     b = B() 
     b.non_static() 

     B.multiplyPeople() 

class B(): 
    @staticmethod 
    def multiplyPeople(): 
     print "this was called" 

    def non_static(self): 
     print self, " was called" 

if __name__ == "__main__": 
    a = A() 

输出:

<__main__.B instance at 0x7f3d2ab5d710> was called 
this was called 
+0

Thx队友简单而精确的欢呼声 –