There is a simple code.

 class User:


     profile = {}
     def __init __ (self, name, lastname, ** profile):
         self.profile ["firstName"] = name
         self.profile ['lastname'] = lastname



         for key, value in profile.items ():
             self.profile [key] = value


     def describe_user (self,):
          for k, v in self.profile.items ():
              print (k, v,)



 user = User ('Peter', 'Smirnov', location = 'Peter', age = "39")


 user.describe_user ()
 class SuperUser (User):
     def __init __ (self, name, lastname, ** profile):
         super () .__ init __ (name, lastname, ** profile)

 superUser = SuperUser ('Semen', 'Semenych', location = 'Sweden', age = '42 ')

 superUser.describe_user ()

Why is the age field initialized in the printout of an instance of a subclass? Moreover, initialized by the value of the instance of the superclass.

    1 answer 1

    Because User.profile one for all instances. You defined a profile at the class level.

    Place the self.profile = {} inside the __init__() method to create a profile for each object.

    • Damn, I can not eat, the object model of the python after java. Did I somehow get a static property? - RVG
    • @RVG Python is not Java. You should not try to use terminology from Java in cases when something does not work (the behavior can be similar, but differ in some detail, hiding the error). Variables at the class level are class attributes that are common to all instances. As with instance attributes , self.profile notation can be used. To create an instance attribute, put self.profile = {} inside the method. docs.python.org/3/reference/compound_stmts.html#class - jfs