Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python inherit variables from parent class

Tags:

python

class

Sorry if I don't explain it that well but I'll try my best:

So I want to inherit the variables from the Parent class, but I don't want to pass them again when creating an instance of the Child class because I think that is redundant. I just want to use the eye color from the Parent for example. See the example code below for what I mean

This is what works:

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


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

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

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

This is what I somehow want:

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)
like image 870
stickfigure4 Avatar asked Mar 02 '17 16:03

stickfigure4


People also ask

How do you inherit values from a parent class in Python?

Add the __init__() Function So far we have created a child class that inherits the properties and methods from its parent. We want to add the __init__() function to the child class (instead of the pass keyword).

How do you access the variables of parent class in a child class in Python?

Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.

Do child classes inherit variables Python?

Child ClassesChild or subclasses are classes that will inherit from the parent class. That means that each child class will be able to make use of the methods and variables of the parent class.

Do child classes inherit variables?

We know every Child class inherits variables and methods (state and behavior) from its Parent class.


1 Answers

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)
like image 84
lelabo_m Avatar answered Sep 29 '22 09:09

lelabo_m