Inheritance: why does self refer to child class?

actual_panda

Sorry I could not come up with a better title.

Please consider this example:

class A:
    def __init__(self, x):
        self.attr = x

class B(A):
    x = 1

    def __init__(self):
        super().__init__(x=self.x)

class C(B):
    x = 2

    def __init__(self):
        super().__init__()


c = C()
print(c.attr) # 2

This code prints 2.

This means self in B.__init__ is an instance of C, not B.

I thought that the purpose of super was to refer to the methods/attributes of the parent class. Can somebody please explain the logic behind the output not being 1? Thank you.

Barmar

The purpose of super() in super().__init__() is to call the __init__() method from the parent. It doesn't change the object itself -- it's still an instance of the subclass. So anything that the subclass overrides will be visible in the instance.

If the parent method doesn't want the overridden value of x, it should use B.x rather than self.x. The whole point of accessing the attribute through self is to take advantage of overriding in subclasses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does self in "class << self" refer to the class?

Why does "this" refer to the parent class although a child class object calls the method?

Java Inheritance and late binding, why does int id have the parent class value and not the child class one?

Does "friending" the base class in CRTP inheritance affect the child class as well?

what does self refer to in Property class creating code?

Why typeof does not check inheritance of class?

How does recursive inheritance work ? class PersonInfoBuilder<SELF> : PersonBuilder where SELF : PersonInfoBuilder<SELF>

Why does the JVM search for a method from child to parent in the inheritance tree?

Why injection does not work in child class?

Why does echo "{self::class}" print the valid class name

Why does C++ forbid private inheritance of a final class?

Why does extend method on class an instance works differently with inheritance?

JAXB Marshalling Child Class inheritance

Java Inheritance: Child class not executing

Why can't reference to child Class object refer to the parent Class object?

self vs class name for class methods in inheritance

Why arrays declared in parent class not visible in child class during inheritance in java?

Under what conditions does PHP 7's self refer to the base class?

What does Java `this` actually refer to in an inheritance situation?

Why does self.class === MyClass return false, while self.class == MyClass returns true?

Why does `class X: mypow = pow` work? What about `self`?

Why does Swift disallow assignment to self in class init, but not in protocol init?

Why does this class descriptor __get__ method return self?

Why does the self in the class always have the previous data?

child class with extra arguments python class inheritance

error in child class definition of templated class inheritance

Why, when calling a function with self on the parent class, the child class is actually run in Python

Why is it necessary to refer to self for the Identifiable protocol?

Does self refer to the ApplicationController, UsersController, or SessionsController?