【问题标题】:Python: Call Method of Instance by another InstancePython:另一个实例调用实例的方法
【发布时间】:2016-04-20 19:45:24
【问题描述】:

我想通过另一个新实例调用一个实例中的方法,如下所示:

class parentClass:
    # Some methods here
    class a:
        def __init__(self, otherclass):
            otherclass.__foo(a);

    class b:
        def __foo(self, firstclass):
            #do something with firstclass

pClass = parentClass();
classB = pClass.b();
classA = pClass.a(classB);

但是使用这段代码我会得到这种错误:

AttributeError: b instance has no attribute '_a__foo'

我已经尝试在方法 __foo() 之前添加它:

@classmethod

但这仍然不起作用。

感谢您的帮助!

【问题讨论】:

    标签: python class methods instance


    【解决方案1】:

    这是最简单的解决方案:

    代替

    def __foo(self, firstclass):
    

    我要写

    def foo(self, firstclass):
    

    双 _ 在这里不起作用。

    我将把这个问答留给有同样“问题”的其他人。

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多