-6
class Person:


    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self,name):
        self.__name = name


    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self,age):
        self.__age = age



    def __init__(self,name,age):

        self.__name = name
        self.__age = age



        pass

    def print_info(self):

        print("my name is {} and my age is {}".format(self.__name,self.__age))

        pass

class Student(Person):

    @property
    def school_name(self):
        return self.__school_name

    @school_name.setter
    def school_name(self,school_name):
        return self.__school_name


    def __init__(self,school_name,name,age):
        Person.__init__(self,name,age)
        self.__school_name = school_name

        pass


    def print_info(self):

        print("my schoolname is {} name is {} age is {}".format(self.__school_name,self.__name,self.__age))
        print("hello")


    pass

if __name__ == '__main__':

    stu = Student("primary school","songpengfei",22)
    stu.print_info()




    pass

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/songpengfei/PycharmProjects/untitled1/review.py", line 69, in <module>
    stu.print_info()
  File "/Users/songpengfei/PycharmProjects/untitled1/review.py", line 60, in print_info
    print("my schoolname is {} name is {} age is {}".format(self.__school_name,self.__name,self.__age))
AttributeError: 'Student' object has no attribute '_Student__name'
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    When you use `__` at the begining of a field name, it is "private" to that class because python does what is called "name mangling. I suggest you google "python name mangling" to learn about how this works. – Code-Apprentice Jan 02 '21 at 10:30
  • Replace the first of ```print_info``` line with ```print("my schoolname is {} name is {} age is {}".format(self.__school_name,self._Person__name,self._Person__age))```. – Henry Tjhia Jan 02 '21 at 10:35
  • I removed what seems to be a politically motivated meme. If that is in any way relevant to your question please translate it and explain its relevance. – Yunnosch Jan 02 '21 at 10:40
  • @HenryTjhia thank you very much ,your answer is correct,It's so complex – spf_tay Jan 02 '21 at 10:44
  • @Code-Apprentice the Henry Tjhia is correct – spf_tay Jan 02 '21 at 10:45
  • @Code-Apprentice sorry ,I understand what you say,thank you very much – spf_tay Jan 02 '21 at 10:54

1 Answers1

0

The short answer is that you should use the property self.name instead of the field self.__name. Similarly, you should only use the name that starts with __ in the @property. Otherwise use the property. So for example, use self.age instead of self.__age outside of the @property.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268