来自另一个类实例的python类实例方法调用

标签 python class python-2.7 oop methods

我有一个关于 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/

相关文章:

python - 在 Python 中解析 Elasticsearch json 输出

python - 在 python 中,对逗号分隔的域名列表执行 DNS 查询的最有效方法是什么?

python - 将列表传递给类中的方法

python - 对列表中的元素子列表进行排序,将其余元素留在原地

python - python urllib中的复选框输入

python - 在 FastAPI 中使用多文件应用程序的配置/依赖关系

python - TensorFlow:损失函数模块化设计

C++ 基于上下文的类实现

c++ - 程序员何时使用空基优化 (EBO)

python - 使用 python 处理 json feed