class User(object):
def __init__(self, name, age):
self.name = name
self.__age = age
def get_age(self):
return self.__age
def __get_name(self):
return self.name
class Student(User):
pass
stu = Student('LiMing', 23)
print(dir(stu))
$ python private_method.py
['_User__age', '_User__get_name', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__'
, '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'get_age', 'name']
$ python --version
Python 3.7.4
私有属性__age
变为_User__age
,而不是_Student__age
私有方法__get_name
变为_User__get_name
,而不是_Student__get_name
请问是什么原因呢?需要怎么修改代码?