7 Replies - 726 Views - Last Post: 04 September 2013 - 01:54 PM Rate Topic: -----

#1 liherb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 25-November 12

Modify parent class __init__() and not override it?

Posted 03 September 2013 - 09:49 PM

I have a parent class and a child class here:

Class Parent:
    def __init__(self):
        self.foo('parent')

    def foo(self, suffix):
        print("I'm a foolish" + suffix)


class Child(Parent):
    def __init__(self):
        self.foo('child')

In this version, Child class simply override its parent class __init__ method altogether. Is there a way for class Child to inherit Parent class. Super() doesn't work because it still invoke original method without modifying it. Thanks.

This post has been edited by andrewsw: 04 September 2013 - 01:51 PM
Reason for edit:: Use CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: Modify parent class __init__() and not override it?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 02:46 AM

If you want your init to do what the parent init does plus some additional stuff, then super does work and is in fact the way to go. If you want to do something altogether different, then overriding it without super like you're doing is the correct thing to do.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 03:28 AM

You're probably looking for:

class Child(Parent):
    def __init__(self):
        Parent.__init__(self)



Now, all the init code will be run on the current child instance (self). This is analogous to super in some other languages.
Was This Post Helpful? 0
  • +
  • -

#4 liherb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 25-November 12

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 11:49 AM

View Postbaavgai, on 04 September 2013 - 03:28 AM, said:

You're probably looking for:

class Child(Parent):
    def __init__(self):
        Parent.__init__(self)



Now, all the init code will be run on the current child instance (self). This is analogous to super in some other languages.



What's the difference between this way and the way using super()? I found it very struggling to figure out.
Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 12:20 PM

super() relies on the MRO (Method Resolution Order), whereas doing the Parent.__init__() route allows you to explicitly specify what is to be called, so you don't end up with unexpected behavior as you would using super() with multiple inheritance.

More on super() in Python

This post has been edited by Valek: 04 September 2013 - 12:21 PM

Was This Post Helpful? 2
  • +
  • -

#6 liherb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 25-November 12

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 12:49 PM

View PostValek, on 04 September 2013 - 12:20 PM, said:

super() relies on the MRO (Method Resolution Order), whereas doing the Parent.__init__() route allows you to explicitly specify what is to be called, so you don't end up with unexpected behavior as you would using super() with multiple inheritance.

More on super() in Python



So can I safely bind Child self to super().__init__(self)? As Parent.__init__(self)
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 01:01 PM

In your code, both methods would work exactly the same since you are utilizing single inheritance.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Modify parent class __init__() and not override it?

Posted 04 September 2013 - 01:54 PM

Yeah, sorry, I often forget there is a super in Python. It's so bloody awkward that it's hardly worth remembering the syntax. I don't believe in multiple inheritance, so it rarely comes up.

Quick example:
class Animal(object):
    def __init__(self, name = None):
        self.name = name

    def say(self):
        if self.name:
            print("I'm a " + self.name)
        else:
            print('Huh?')

class Cat(Animal):
    def __init__(self):
        super(Cat, self).__init__('Cat')

class Dog(Animal):
    def __init__(self):
        Animal.__init__(self, 'Dog')

class Shark(Animal):
    def say(self):
        print("Room Service")

for x in [ Cat(), Dog(), Shark() ]:
    x.say()



We achieve the same results three different ways; granted, the last is messy. The thing about Python is that it's flexible enough to free you from some class based OO paradigms, if you prefer. It's also not rigid enough to enforce some OO behaviors. It's kind of what I like about it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1