0

First things first, I'm reasonably new to python, but I have been working hard and doing lots of tutorials and sample projects to get better, so, if I'm missing something obvious, I appologize.

I've been trying to figure this out for a while now, and I've done a number of searches here and through the googles, but I can't quite figure out how to turn the examples I've found into what I'm looking for, so I was hoping someone here could give me a push in the right direction.

class Super1:
    def __init__(self,artib1,atrib2,atrib3):
        self.atrib1 = atrib1
        self.atrib2 = atrib2
        self.atrib3 = atrib3

class Sub1(Super1):
    def __init__(self,atrib4,atrib5,atrib6)
        self.atrib4 = atrib4
        self.atrib5 = atrib5
        self.atrib6 = atrib6

okay, so what I'm having trouble figuring out is, in the tutroials I've done, they said that I could call on the class like this:

spam = Super1("eggs","foo","bar")

and if I input

print spam.atrib1

it would spit out

eggs

What I want to do is make spam = Sub1, but I don't know how to call it so that I can set all the 'attrib's the way I did with Super1.

I looked up a number of 'multiple inheritance' examples, but I can't seem to reconcile the examples into my own needs. Most of the tutorials don't have more than 1 atribute, or often have the sub 'override' the atributes of the super.

I also checked into composition, and I'm not sure that's exactly what I'm looking for for this part of my project, but I do know that I will need it in later parts.

If anyone can point me in the right direction, that would be great.

1
  • 3
    You're not calling the superclass constructor, and you're not taking the arguments needed for that constructor. Commented Aug 30, 2017 at 17:02

4 Answers 4

3
  1. You need to call the parent class's constructor Super1.__init__(self)
  2. You also need to allow Sub1 to take the arguments for the parent class's constructor.

With the modifications above, your code becomes:

class Sub1(Super1):
    def __init__(self, artib1, atrib2, atrib3, atrib4, atrib5, atrib6)
        Super1.__init__(self, artib1, atrib2, atrib3)
        self.atrib4 = atrib4
        self.atrib5 = atrib5
        self.atrib6 = atrib6

However, rather than calling the parent class's constructor yourself, you should use the super built-in function:

super(Sub1, self).__init__(artib1, atrib2, atrib3)

That way, you don't have to hard-code the name of the parent class in each sub-classes constructor. This allows you to easily refactor your code. Another added benefit of using super is that will automatically deal with the sticky details of multiple-inheritance problems such as "diamond inheritance".

One more piece of advice is that if you don't know the amount of positional arguments ahead of time that the super class will take, you can use the *args syntax:

class Sub1(Super1):
        def __init__(self, atrib4, atrib5, atrib6, *args)
            super(Sub1, self).__init__(*args)
            self.atrib4 = atrib4
            self.atrib5 = atrib5
            self.atrib6 = atrib6
Sign up to request clarification or add additional context in comments.

2 Comments

That looks like exactly what I need! Thank you very much!!
Glad to help out @WardSeabrook :-) If this answer helped, consider accepting it.
1

If Sub1 inherits from Super1, that's supposed to mean it is a Super1 (with some extra stuff added, or with some customizations). But you can't remove things, so Sub1 must

  1. contain everything a Super1 contains
  2. initialize the Super1 part of itself by calling super(Sub1,self).1.__init__(self, ...) in its own constructor.

So, if you your super class has a member a, whose value is passed to its constructor, your subclass also has (inherits) a member a, and must somehow pass its value to the superclass constructor.

Whether that means

class Sub1(Super1):
    def __init__(self, a, b, c, d, e, f):
        super(Sub1, self).__init__(a,b,c)
        self.d=d
        self.e=e
        self.f=f

or whether there's some relationship between the super and subclass arguments (or the subclass hard-codes some of the superclass arguments, or ...) depends on your code.

Comments

0

If you call spam = Super1("eggs","foo","bar"). It will call Super class constructor.

2 Comments

No, it will not. There is no Super class.
I don't really understand what you're suggesting here. I did call spam = Super1("eggs","foo","bar") what I want to do is call spam = Sub1("eggs","foo","bar") but also set atrib1,atrib2,atrib3 at the same time.
0

The problem is if you want to create a instance for the Sub1 you should spam = Super1("eggs","foo","bar",atrib4,atrib5,atri6). Also you have to change the constructor for the Sub1 as:

def __init__(self,atrib1,atrib2,atrib3,atrib4,atrib5,atrib6):
     Super1.__init__(self,atrib1,atrib2,atrib3)
     self.atrib4 = atrib4
     self.atrib5 = atrib5
     self.atrib6 = atrib6`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.