我一直在寻找这个问题的答案,但还没有运气。我需要让子类访问其父类中属性的值。
这是一个例子:
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 <module>
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()。
当班级不是孩子时,我已经找到了如何做到这一点,但我想将这两个班级保密。
有任何想法吗?
提前致谢!