N
N
nevro2018-09-06 13:37:18
Python
nevro, 2018-09-06 13:37:18

What happens when super() is called on a child?

Hello.

class Person():
   def __init__(self, name):
         self.name = name

class MDPerson(Person):
   def __init__(self, name):
        self.name = name
        super.__init__(name)

mdperson = MDPerson('Fudd')

Does Python only create an object of class MDPerson before assignment? Or an object of the Person class? I don’t understand what will happen if we assign a name in MDPerson, and also call super () for the parent. It is not possible for 2 name variables to exist at the same time in the same object of the MDPerson class. Then there will be either overwriting, or one variable each in the object of the MDPerson and Person class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gimntut, 2018-09-06
@gimntut

Person will create a name field, because the ancestor did not have such a field.
MDPerson will use the name field that it inherited.
If you call super, then the Person method will be called, which will change the same variable.
An object of the MDPerson class is also an object of the Person class and other ancestors, since MDPerson inherited all abilities from Person, so it can be used in the same place as objects of the Person class.

print(isinstance(mdperson, Person), isinstance(mdperson, MDPerson) )

For your information: https://habr.com/post/62203/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question