0

I have this code:

class Pere():
    def __init__(self, nom):
        self.nom = nom
    def yeux(self):
        print 'les yeux bleus'

class Mere():
    def __init__(self, nom):
        self.nom = nom
    def taille(self):
       print 'je suis longue!'

class Enfant(Pere, Mere):
    pass

And in some tutorials speaking about inheritance, they use ParentClass.__init__(self, *args) for the child constructor.

Here is the example where it is used:

class Person(object):
    def __init__(self, nom, age):
        self.name = nom
        self.age = age
    def __str__(self):
        return 'je suis {0}, et j\'ai {1} ans'.format(self.name, self.age)

class Militaire(Person):
    def __init__(self, nom, age, grade):
        Person.__init__(self, nom, age)
        self.grade = grade
    def __str__(self):
        return Person.__str__(self) + ' et je suis un {0}'.format(self.grade)

When to use it?

In multiple inheritance, we dont need it (write it twice for example if it exists)?

Abdelouahab
  • 7,331
  • 11
  • 52
  • 82

1 Answers1

3

The unbound-method call (Parent.__init__(self, ...)) does not work well in general with multiple inheritance -- just like simply inheriting __init__ (if delegating to the parent is all you do in the method in your subclass, don't -- just omit defining the method in the subclass so it inherits from the superclass).

In your example that's totally moot since both superclasses have identical implementations of __init__ (in the original Q, now edited to change that), so it doesn't really matter what you do.

More generally, that's what super was introduced for -- it works smoothly in Python 3, a bit less in Python 2 (in the latter case, only for new-style classes, normally inheriting from object -- but then nobody should be defining old-style classes any more!-), but it still works for multiple inheritance much better than older approaches such as explicitly calling Parent.__init__ (it only works well if every class in the hierarchy cooperates, though).

See e.g Understanding Python super() with __init__() methods for more about super and the like.

Community
  • 1
  • 1
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • I am using `2.7`, so, i have to inherit from `Object` for both parent classes to make `super` work? Edit: added a new example – Abdelouahab Jan 16 '15 at 14:53
  • 1
    @Abdelouahab, yes, with Python 2 you need to subclass `object` (or have a global `__metaclass__=type` at the start of your module). Please tag your Python Qs as being Python 2 or 3 respectively, if they are specific to only one version. – Alex Martelli Jan 16 '15 at 14:59
  • modified the example and the tag – Abdelouahab Jan 16 '15 at 15:02
  • so the job of `Parent.__init__(self, ...)` is to repeat writing `self.arg = arg` ? – Abdelouahab Jan 16 '15 at 15:11
  • 1
    @Abdelouahab, it's to let superclasses do whatever initialization work they do -- `self.arg = arg` is only one special case though pretty common. – Alex Martelli Jan 16 '15 at 15:12
  • it seems that i have a lot to learn with OOP in python, because it is far from being from the easy world of procedural, espetially with this distinction with `py3` – Abdelouahab Jan 16 '15 at 15:16
  • 1
    @Abdelouahab, just avoid multiple inheritance and it becomes crystal simple -- lots of Pythonistas prefer to avoid MI anyway! – Alex Martelli Jan 16 '15 at 15:20
  • ah! thank you for the device, because this is like i switched another language when it came to OOP! thank you again – Abdelouahab Jan 16 '15 at 15:22