28

Let's say I have the following classes set up:

class Foo:
     def __init__(self, frob, frotz):
          self.frobnicate = frob
          self.frotz = frotz
class Bar:
     def __init__(self, frob, frizzle):
          self.frobnicate = frob
          self.frotz = 34
          self.frazzle = frizzle

How can I (if I can at all) use super() in this context to eliminate the duplicate code?

Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
lfaraone
  • 49,562
  • 17
  • 52
  • 70
  • [http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python](http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python) – Felipe Hoffa Jul 23 '09 at 19:57

2 Answers2

29

Assuming you want class Bar to set the value 34 within its constructor, this would work:

class Foo(object):
     def __init__(self, frob, frotz):
          self.frobnicate = frob
          self.frotz = frotz

class Bar(Foo):
     def __init__(self, frob, frizzle):
          super(Bar, self).__init__(frob, frizzle)
          self.frotz = 34
          self.frazzle = frizzle


bar = Bar(1,2)
print "frobnicate:", bar.frobnicate
print "frotz:", bar.frotz
print "frazzle:", bar.frazzle

However, super introduces its own complications. See e.g. super considered harmful. For completeness, here's the equivalent version without super.

class Foo(object):
     def __init__(self, frob, frotz):
          self.frobnicate = frob
          self.frotz = frotz

class Bar(Foo):
     def __init__(self, frob, frizzle):
          Foo.__init__(self, frob, frizzle)
          self.frotz = 34
          self.frazzle = frizzle


bar = Bar(1,2)
print "frobnicate:", bar.frobnicate
print "frotz:", bar.frotz
print "frazzle:", bar.frazzle
Asclepius
  • 57,944
  • 17
  • 167
  • 143
ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • 1
    why are you setting Bar.frotz = frizzle through the super class constructor then setting it to 34 seperately? shouldn't you just have __init__(frob,34)? – Victor Jul 23 '09 at 20:03
  • 2
    Yes, that would work too, and would be cleaner. The example is rather contrived (setting a constant in a constructor), but I was trying to stay as close to the original as possible. – ire_and_curses Jul 23 '09 at 20:58
  • i don't know if that's a good way to do it... consider the case where the super constructor performs an operation on frotz and frizzle is a class, your code won't run or much worse things could happen. – Victor Jul 23 '09 at 23:23
  • 1
    See Super considered super! - PyCon 2015 https://www.youtube.com/watch?v=asrMKBVHtSw – Chris Kavanagh Oct 05 '16 at 07:55
26

In Python >=3.0, like this:

class Foo():
    def __init__(self, frob, frotz)
        self.frobnicate = frob
        self.frotz = frotz

class Bar(Foo):
    def __init__(self, frob, frizzle)
        super().__init__(frob, 34)
        self.frazzle = frizzle

Read more here: http://docs.python.org/3.1/library/functions.html#super

EDIT: As said in another answer, sometimes just using Foo.__init__(self, frob, 34) can be the better solution. (For instance, when working with certain forms of multiple inheritance.)

Asclepius
  • 57,944
  • 17
  • 167
  • 143
JAB
  • 20,783
  • 6
  • 71
  • 80
  • 9
    Just to be clear, this is only python >= 3.0. For older pythons (but only those with "new-style classes"), @ire_and_curses answer describes what needs to be done. – Dav Clark Mar 28 '11 at 03:25