无法在父方法中调用子属性,测试如下:
#!/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())
Run Code Online (Sandbox Code Playgroud)
运行 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'
Run Code Online (Sandbox Code Playgroud)