【发布时间】:2017-10-26 19:52:57
【问题描述】:
不能在父方法中调用子属性,这里是测试:
#!/usr/bin/env python3
class A():
def getPath(self):
return self.__path
class B(A):
def __init__( self, name, path):
self.__name = name
self.__path = path
instance = B('test', '/home/test/Projects')
print(instance.getPath())
运行python测试文件$ ./test.py返回
./test.py
Traceback (most recent call last):
File "./test.py", line 17, in <module>
print(instance.getPath())
File "./test.py", line 6, in getPath
return self.__path
AttributeError: 'B' object has no attribute '_A__path'
【问题讨论】:
标签: python-3.x inheritance methods attributes parent