嘿,伙计们需要知道如何在B类的classA中启动一个方法
有
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 :(得分: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