You should call __init__ from the parent class:

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

class Teacher(Person):
    def __init__(self, name, occupation, subject):
        Person.__init__(self, name, occupation)
        self.subject = subject
Answer from Daniel on Stack Overflow
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Inheritance › inheritVarsAndMethods.html
22.2. Inheriting Variables and Methods — Foundations of Python Programming
So if you wanted to define a Dog class as a special kind of Pet, you would say that the Dog type inherits from the Pet type. In the definition of the inherited class, you only need to specify the methods and instance variables that are different from the parent class (the parent class, or the ...
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
Python has two built-in functions that work with inheritance: Use isinstance() to check an instance’s type: isinstance(obj, int) will be True only if obj.__class__ is int or some class derived from int.
Discussions

How does python variable inheritance work? - Stack Overflow
In python when you override a function which was supposed to be inherited you override all of it, __init__ is no exception. More on stackoverflow.com
🌐 stackoverflow.com
Inherited class variable modification in Python - Stack Overflow
I'd like to have a child class modify a class variable that it inherits from its parent. I would like to do something along the lines of: class Parent(object): foobar = ["hello"] class Child( More on stackoverflow.com
🌐 stackoverflow.com
Confused about class variables, also with inheritance
The class variables and instance variables remain separate if an instance variable of the same name is created. One doesn't override the other, but the class variable will no longer be readable via the object: In [1]: class c: ...: a = 1 ...: def __init__(self): ...: self.a = 2 ...: In [2]: obj = c() In [3]: c.a Out[3]: 1 In [4]: obj.a Out[4]: 2 As for inheritance, yes if you assign a value to a class variable on a child that has the same name as one of the parent's class variables, then a separate class variable of that name will be created for the child. Again, the two will remain separate, one readable from the parent and one readable from the child, but the parent's variable will no longer be visible via the child class. More on reddit.com
🌐 r/learnpython
10
17
February 24, 2024
inheritance - Inheriting from instance in Python - Stack Overflow
In Python, I would like to construct an instance of the Child's class directly from an instance of the Parent class. For example: A = Parent(x, y, z) B = Child(A) This is a hack that I thought might More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › python_inheritance.asp
Python Inheritance
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function. Python also has a super() function that will make the child class inherit all the methods and properties from its parent:
🌐
Reddit
reddit.com › r/learnpython › confused about class variables, also with inheritance
r/learnpython on Reddit: Confused about class variables, also with inheritance
February 24, 2024 -

I'm finding class variables super confusing. I thought they were just like static variables in C++ (which instances can't own), but it seems they have quite different behavior.

Basic Class variable behavior - is this correct?

class Foo:
  x: int
  • if you set Foo.x, it overrides the value of .x for all instances of Foo, but

  • if you set .x on an instance of Foo, it only changes .x on that instance.

edit: actually this can't be the full story, because sometimes changing Foo.x doesn't change the instance's .x??

Class variables + inheritance, what is going on?

class Parent:
  x: int

class Child(Parent):
  pass

Parent.x = 1      
print(Child.x)        # = 1. ok so child inherits parent class variable

Child.x = 2
print(Parent.x)       # = 1. ok, so child cannot set parent class variable

Parent.x = 3
print(Child.x)         # = 2. hol' up, now child doesn't inherit from parent anymore?

Also, if multiple classes inherit from Parent, if I set Child1.x does it affect the other children? How are instances affected too?

Class variables without declaration works too...?

What's the point of defining these variables in the class body if you don't need to?

class Foo:
   pass

Foo.x = 3

I feel like there's some kind of mental model for class variables I'm just not understanding. Is there any easy way to think about them? Also is there any other weird behavior I should know?

Top answer
1 of 5
5
The class variables and instance variables remain separate if an instance variable of the same name is created. One doesn't override the other, but the class variable will no longer be readable via the object: In [1]: class c: ...: a = 1 ...: def __init__(self): ...: self.a = 2 ...: In [2]: obj = c() In [3]: c.a Out[3]: 1 In [4]: obj.a Out[4]: 2 As for inheritance, yes if you assign a value to a class variable on a child that has the same name as one of the parent's class variables, then a separate class variable of that name will be created for the child. Again, the two will remain separate, one readable from the parent and one readable from the child, but the parent's variable will no longer be visible via the child class.
2 of 5
4
If you do an attribute lookup on an instance and the attribute isn't found on the instance itself, it will look on the class next. If you do an attribute lookup on a class and it isn't found on the class itself, it will look on the parent class next. If you do an attribute assignment, however, that always sets the attribute directly on the thing itself. That means, if you do an attribute assignment on an instance, even if its class already has an attribute of that name, you create a new instance attribute rather than reassigning the class attribute. Importantly, a lookup of that attribute on the instance would now no longer propagate up to the class. The same is true for classes and their parent classes. This becomes clearer if you take a look at the special __dict__ attribute of your classes and objects as you do the assignments, which holds all direct attributes of the object or class in dictionary form.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-class-inheritance-in-python-3
Understanding Class Inheritance in Python 3 | DigitalOcean
August 20, 2021 - We have overridden the initialized parameters in the __init__() method, so that the last_name variable is now set equal to the string "Shark", the skeleton variable is now set equal to the string "cartilage", and the eyelids variable is now set to the Boolean value True. Each instance of the class can also override these parameters.
Find elsewhere
Top answer
1 of 8
15

I find this (encapsulation) to be the cleanest way:

class Child(object):
    def __init__(self):
        self.obj = Parent()

    def __getattr__(self, attr):
        return getattr(self.obj, attr)

This way you can use all Parent's method and your own without running into the problems of inheritance.

2 of 8
11

OK, so I didn't realize that you where happy with a static copy of the arguments until I was already halfway done with my solution. But I decided not to waste it, so here it is anyway. The difference from the other solutions is that it will actually get the attributes from the parent even if they updated.

_marker = object()

class Parent(object):

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

class Child(Parent):

    _inherited = ['x', 'y', 'z']

    def __init__(self, parent):
        self._parent = parent
        self.a = "not got from dad"

    def __getattr__(self, name, default=_marker):
        if name in self._inherited:
            # Get it from papa:
            try:
                return getattr(self._parent, name)
            except AttributeError:
                if default is _marker:
                    raise
                return default

        if name not in self.__dict__:
            raise AttributeError(name)
        return self.__dict__[name]

Now if we do this:

>>> A = Parent('gotten', 'from', 'dad')
>>> B = Child(A)
>>> print "a, b and c is", B.x, B.y, B.z
a, b and c is gotten from dad

>>> print "But x is", B.a
But x is not got from dad

>>> A.x = "updated!"
>>> print "And the child also gets", B.x
And the child also gets updated!

>>> print B.doesnotexist
Traceback (most recent call last):
  File "acq.py", line 44, in <module>
    print B.doesnotexist
  File "acq.py", line 32, in __getattr__
    raise AttributeError(name)
AttributeError: doesnotexist

For a more generic version of this, look at the http://pypi.python.org/pypi/Acquisition package. It is in fact, in some cases a bloody need solution.

🌐
GitHub
gist.github.com › 5610137
python inherit instance variable · GitHub
May 20, 2013 - Save wyukawa/5610137 to your computer and use it in GitHub Desktop. Download ZIP · python inherit instance variable · Raw · gistfile1.py · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Python Forum
python-forum.io › thread-30898.html
Can we access instance variable of parent class in child class using inheritance
I am pasting my code below:- class A: def __init__(self,i=100): self.i=100 class B(A): def __init__(self,j=0): self.j=j b=B() print(b.j) print(b.i)I have 2 class A and B . B is inheriting class A so a
🌐
Codecademy
codecademy.com › forum_questions › 560afacd86f552c8a70001dd
9/11 Inheritance. Don't Child Classes inherit Parent Variables? | Codecademy
... According to what we’ve been told, a Child Class should inherit properties from its Parent Class. So, I’d expect that trying this · my_car = ElectricCar('Olds Mobile', 'matte black', 'molten salt', 0) Would be okay and I’d lead me ...
🌐
Tutorial Teacher
tutorialsteacher.com › python › inheritance-in-python
Inheritance in Python
Instance attributes and methods defined in the parent class will be inherited by the object of the child class. To demonstrate a more meaningful example, a quadrilateral class is first defined, and it is used as a base class for the rectangle class.
Top answer
1 of 2
3

Any property of Dog will override a property inherited from Cat. You can re-define a value in Cat, but it won't matter because it has already been overridden by the child. For example:

class Cat:
    age = 0  # Cat.age = 0


class Dog(Cat):
    pass  # Dog.age = Cat.age = 0


Dog.age=1  # Dog.age = 1, and Dog.age no longer points to Cat.age
Cat.age=2  # Cat.age = 2

print(Dog.age, Cat.age)  # Dog.age is no longer Cat.age. They are completely different

Contrast that with this:

class Cat:
    age = 0  # Cat.age = 0


class Dog(Cat):
    pass  # Dog.age = Cat.age = 0

Cat.age = 10  # Cat.age = 10

print(Dog.age, Cat.age)  # Dog.age points to Cat.age, so Dog.age resolves to 10
2 of 2
2

Inheritance refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class. The class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class(es), and a method can call the method of a base class with the same name.

class Cat:
    def __init__(self):
        self.age = 2
        self.sound = "meow"


class Dog(Cat):
    def __init__(self):
        super().__init__()
        self.sound = "bark"


cat = Cat()
dog = Dog()

print(f"The cat's age is {cat.age}, and the dog's age is {dog.age}.")
print(f"Cats {cat.sound}, and dogs {dog.sound}.")

The cat's age is 2, and the dog's age is 2.

Cats meow, and dogs bark.


So, you can see the dog.age can inherit from class Cat. Notice the sound part, the method in the derived class overrides that in the base class. This is to say, we wrote the dog sound, it gets preference over the class Cat sound.

🌐
PYnative
pynative.com › home › python › python object-oriented programming (oop) › python class variables
Python Class Variables With Examples – PYnative
September 8, 2023 - We can access class variables by class name or object reference, but it is recommended to use the class name. In Python, we can access the class variable in the following places · Access inside the constructor by using either self parameter or class name. Access class variable inside instance method by using either self of class name
Top answer
1 of 2
4

What you ask does not make sense:

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender):
        super().__init__(eye_color, length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men")

print(x.length, x.eye_color)
print(y.gender, x.length)

In child, the parameters eye_color and length come from nowhere.

rassar example is good if you want to reuse a parent object.

You can also do the following:

class Parent:
    # def __init__(self, eye_color=(default value here), length=(default value here)):
    def __init__(self, eye_color="", length=0):
        self.eye_color = str(eye_color)
        self.length = length

OR

class Parent:
    def __init__(self, eye_color="", length=0):
        self.eye_color = str(eye_color)
        self.length = length

class Child(Parent):
    def __init__(self, gender, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men") # Work with first example of Parent
y = Child("Men", eye_color="Blue", length=2) # Work with first
y = Child("Men", "Blue", 2) # Work with second example

print(x.length, x.eye_color)
print(y.gender, y.length)
2 of 2
2

You could try passing a Parent instance to the Child initializer...That's probably the closest you'll get.

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender, parent):
        super().__init__(parent.eye_color, parent.length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men", x)

print(x.length, x.eye_color)
print(y.gender, x.length)

Another thing you could do is hold a last_parent variable:

global last_parent

class Parent:
        def __init__(self, eye_color, length):
            self.eye_color = str(eye_color)
            self.length = length
            last_parent = self


class Child(Parent):
    def __init__(self, gender):
        super().__init__(last_parent.eye_color, last_parent.length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men")

print(x.length, x.eye_color)
print(y.gender, x.length)
🌐
LabEx
labex.io › tutorials › python-how-to-customize-inheritance-with-class-variables-452343
How to customize inheritance with class variables | LabEx
Avoid modifying class variables directly in instance methods · Use @classmethod for operations that involve class-level data ... By understanding class variables, Python developers can create more efficient and organized code structures in LabEx programming environments. Inheritance allows classes to inherit attributes and methods from parent classes.
🌐
Medium
medium.com › @gauravverma.career › inheritance-in-python-a7aaf1d41971
Inheritance in Python | by Gaurav Verma | Medium
December 7, 2025 - This is because Python will inherit the __init__() method from the parent class if it is not explicitly overridden in the child class. If the child class has its own constructor, you can call the base class constructor explicitly using either ...