6

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)
3
  • You can't get what you want. But you can use *args, **kwargs to almost get there. Commented Mar 2, 2017 at 16:47
  • 2
    eye_color and length are specific to an instance of Parent, not the class as a whole. Given x1 = Parent("Blue", 2), x2 = Parent("Green", 3), and y = Child("Men"), what should the value of y.eye_color or y.length be? At the very least, Child.__init__ would need to take a Parent instance as an argument. Commented Mar 2, 2017 at 16:47
  • You can create a create_child method in Parent class that will return an instance of Child class with parent characteristics. So you can use it like y = x.create_child() Commented Mar 2, 2017 at 16:49

2 Answers 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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you this is really helpful, but I think rassar solution is a better approach for me, anyway I'll try coding and see what's best to use.
To be fair, maybe OP's question does not make sense but I found this page looking exactly for this solution because this is something inheritance in C++ will do.
Sorry, I did not mean the question was stupid, just that the example of what it was trying to do in python made no sense because some variables were not passed through.
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)

1 Comment

Thanks your solution is very helpful! I think I'll go with this approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.