Python: 另一个实例调用实例的方法
Python: Call Method of Instance by another Instance
我想通过另一个新实例调用实例中的方法,如下所示:
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
但这还是不行。
感谢帮助!
这是最简单的解决方案:
而不是
def __foo(self, firstclass):
我要写
def foo(self, firstclass):
双 _ 在这里不起作用。
我会把这个问答留在这个地方给其他有相同问题的人 'problem'。
我想通过另一个新实例调用实例中的方法,如下所示:
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
但这还是不行。
感谢帮助!
这是最简单的解决方案:
而不是
def __foo(self, firstclass):
我要写
def foo(self, firstclass):
双 _ 在这里不起作用。
我会把这个问答留在这个地方给其他有相同问题的人 'problem'。