私有化方法和私有化属性的本质是相同的,不同之处是表现在内存中存储的数据不一样,一个是值,一个是方法。
class Dog(object):
__name = 'Tom'
def eat(self):
print('Tom在吃东西')
def __run(self):
return 'Tom在跑步'
d = Dog()
# print(Dog.__name) # AttributeError: 'Dog' object has no attribute '__name'
d.eat()
# d.__run() # AttributeError: 'Dog' object has no attribute '__eat'
print(Dog.__dict__) # _Dog__run 和私有属性一样被重名