我一直在寻找这个问题的答案,但还没找到。我需要一个子类访问其父类中属性的值。在
下面是一个例子:class myclass:
def __init__(self):
class __firstinternal__:
def __init__(self):
self.second = __secondinternal__()
class __secondinternal__:
def __init__(self):
self.var = myclass.getString()
def getString(self):
return "something"
self.internal = __firstinternal__()
print self.internal.second.var
c = myclass()
Traceback (most recent call last):
File "./inheritest.py", line 19, in
c = myclass()
File "./inheritest.py", line 15, in __init__
self.internal = __firstinternal__()
File "./inheritest.py", line 7, in __init__
self.second = __secondinternal__()
File "./inheritest.py", line 11, in __init__
self.anothervar = myclass.getString()
AttributeError: class myclass has no attribute 'getString'
换句话说,我需要secondinternal从父myclass继承getString()方法。在
我已经找到了当这个班不是孩子的时候该怎么做,但是我想让这两个班保持隐私。在
有什么想法吗?在
提前谢谢!在