我有一个关于 python 类的快速问题。 以下是设置: 我有一个类作为“策划者”类,其中包含其他类的各种实例。现在这些类需要调用另一个类的方法,但我不知道该怎么做。例如:
class mastermind(object):
def __init__(self):
self.hand = hand()
def iNeedToCallThisMethod(self, funzies):
print funzies
class hand(object):
def __init(self):
pass #here should be a call to the mastermind instance's method
example = mastermind()
希望你们能帮我解决这个问题,我的脑子里冒着热气!非常感谢!
最佳答案
如果你想调用mastermind的方法,你需要有对它的引用。
例如
class mastermind(object):
def __init__(self):
self.hand = hand(self)
def iNeedToCallThisMethod(self, funzies):
print funzies
class hand(object):
def __init__(self, mastermind)
mastermind.iNeedToCallThisMethod('funzies')
关于来自另一个类实例的python类实例方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463418/