小编Alp*_*ket的帖子

python 3 从父方法调用子属性

无法在父方法中调用子属性,测试如下:

#!/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)

methods inheritance attributes parent python-3.x

2
推荐指数
1
解决办法
4200
查看次数

标签 统计

attributes ×1

inheritance ×1

methods ×1

parent ×1

python-3.x ×1