class A: __x = 1 # _A__x = 1 def __init__(self,name): self.__name=name # self._A__name = name def __foo(self): # _A__foo print('run foo')# print(A.__dict__)'''{'__module__': '__main__', '_A__x': 1, '__init__': <function A.__init__ at 0x0000017B28A1F950>, '_A__foo': <function A.__foo at 0x0000017B28A1F488>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}''''''这种变形的特点: 1、外部无法直接使用:obj.__AttrName 2、在类内部是可以直接使用:obj.__AttrName 3、子类无法覆盖父类__开头的属性'''class A: def foo(self): print('A foo') def bar(self): print('A bar') self.foo()class B(A): def foo(self): print('b foo')b1 = B()b1.bar()# 结果# A bar# b fooclass A: def __foo(self): print('A foo') def bar(self): print('A bar') self.__foo()class B(A): def __foo(self): print('b foo')b1 = B()b1.bar()# 结果# A bar# A foo
来源:https://www.cnblogs.com/kingforn/p/11340155.html