Private Variable and Private Method - Python 私有变量 和 私有方法

时间:2023-03-09 09:55:16
Private Variable and Private Method - Python 私有变量 和 私有方法
 Private Variable and Private Method
   Python 不象 Java 那样, 通过 private 关键字创建私有属性, python 通过更简洁的实现了'私有属性', 从而避免了子类意外覆盖私有属性.
举个例子来说, 现在编写一下儿名字叫 Robot 的类,并实现了一个名字为 fighting 的属性。
接着又人编写了一个叫 Camaro 的类, 并继承了 Robot 类, 并在其中构造了 fighting 的属性, 这个时候 Robot 的 fighting 属性被覆盖了.
而恰巧 Robot 类的编写者的意图是 - 不允许所有 Robot 的子类访问 fighting(可能为了世界和平), 显然 Camaro 这个子类破坏了这一点. 为了避免这种情况, Robot 类的编写者可以以 __fighting (或者 __fighting_) 来命名要私自属性(声明成成私有属性).
这时 Python 的解释器会该属性 '修饰' 后再添加到 __dict__ 属性中, 该例中会变成 _Robot__fighting, 而不是 __fighting (原形式)
Python 的这种特性被称为 name mangling.
注, 名称改写(name mangling),Python 解释器在运行时自动把私有属性 __x (或 __x_)重命名为 _MyClass__X (或 _MyClass__X_)

以上文字所对应的例子,
class Robot(object):
__RB = 3
__RB_ = 5
def __fighting(self):
print('This is \'_MyClass__X\' format') def __fighting_(self):
print('This is \'_MyClass__X_\' format') def func(self):
print('Called via - A') class Camaro(Robot):
def func(self):
print('Called via - B') if __name__ == "__main__":
print(Robot.__dict__) #
a = Camaro()
a.func() #
print(Camaro.__mro__) #
super(Camaro,a).func() #
a._Robot__fighting() #
a._Robot__fighting_() #
a.__fighting # Output,
{'__module__': '__main__', '_Robot__RB': 3, '_Robot__RB_': 5,'_Robot__fighting': <function Robot.__fighting at 0x039C6300>,
'_Robot__fighting_': <function Robot.__fighting_ at 0x039C62B8>,
'func': <function Robot.func at 0x039C6270>, ...}
#1 |^ 添加到 __dict__ 中的是 '_MyClass__X' 或者 '_MyClass__X_' - name mangling
Called via - B #2 子类的中方法覆盖了父类中的'同名'方法
(<class '__main__.Camaro'>, <class '__main__.Robot'>, <class 'object'>) #3 MRO __mro__
Called via - A #3 super 指的并不一定是'父类', 而是 MRO 中的'下一个' ******
This is '_MyClass__X' format #4 name mangling ; 知道 name mangling 规则后 可以 hack 私有属性
This is '_MyClass__X_' format #4 name mangling ; 知道 name mangling 规则后 可以 hack 私有属性
a.__fighting
AttributeError: 'Camaro' object has no attribute '__fighting' #5 常规方法调用属性报错(AttributeError), 因为 __dict__ 没有相应属性