16

I'm playing with Python Class inheritance and ran into a problem where the inherited __init__ is not being executed if called from the sub-class (code below) the result I get from Active Python is:


>>> start
Tom Sneed
Sue Ann
Traceback (most recent call last):
  File "C:\Python26\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, <br>in RunScript
    exec codeObject in __main__.__dict__
  File "C:\temp\classtest.py", line 22, in <module>
    print y.get_emp()
  File "C:\temp\classtest.py", line 16, in get_emp
    return self.FirstName + ' ' + 'abc'
AttributeError: Employee instance has no attribute 'FirstName'

Here's the code

class Person():
    AnotherName = 'Sue Ann'
    def __init__(self):
        self.FirstName = 'Tom'
        self.LastName = 'Sneed'

    def get_name(self):
        return self.FirstName + ' ' + self.LastName

class Employee(Person):
    def __init__(self):
        self.empnum = 'abc123'

    def get_emp(self):
        print self.AnotherName
        return self.FirstName + ' ' + 'abc'

x = Person()
y = Employee()
print 'start'
print x.get_name()
print y.get_emp()
S.Lott
  • 384,516
  • 81
  • 508
  • 779
meade
  • 22,875
  • 12
  • 32
  • 36

4 Answers4

25

Three things:

  1. You need to explicitly call the constructor. It isn't called for you automatically like in C++
  2. Use a new-style class inherited from object
  3. With a new-style class, use the super() method available

This will look like:

class Person(object):
    AnotherName = 'Sue Ann'
    def __init__(self):
        super(Person, self).__init__()
        self.FirstName = 'Tom'
        self.LastName = 'Sneed'

    def get_name(self):
        return self.FirstName + ' ' + self.LastName

class Employee(Person):
    def __init__(self):
        super(Employee, self).__init__()
        self.empnum = 'abc123'

    def get_emp(self):
        print self.AnotherName
        return self.FirstName + ' ' + 'abc'

Using super is recommended as it will also deal correctly with calling constructors only once in multiple inheritance cases (as long as each class in the inheritance graph also uses super). It's also one less place you need to change code if/when you change what a class is inherited from (for example, you factor out a base-class and change the derivation and don't need to worry about your classes calling the wrong parent constructors). Also on the MI front, you only need one super call to correctly call all the base-class constructors.

workmad3
  • 25,101
  • 4
  • 35
  • 56
  • 4
    Care must be taken when using super, please read this: http://fuhm.net/super-harmful/ – Martin Geisler May 29 '09 at 20:41
  • +1 for recommending new style classes, particularly as the OP's stack trace suggests they're using Python 2.6. – Jarret Hardie May 29 '09 at 20:41
  • 1
    is there an issue if the super class does not have an init? and if so does that mean the super class can not be a 'black box'? – meade May 29 '09 at 20:49
  • No issue of the superclass has no __init__. It's pretty black-box-ish. – S.Lott May 29 '09 at 21:14
  • Good to know about the pitfalls. I've been pretty careful so far though and all my python code calls super() all the way back up to object :) – workmad3 May 29 '09 at 22:11
9

You should explicitely call the superclass' init function:

class Employee(Person):
    def __init__(self):
        Person.__init__(self)
        self.empnum = "abc123"
Martin Cote
  • 28,864
  • 15
  • 75
  • 99
  • That worked - but why? why would the inherited class not work the same as if you instantiated it as its own object? – meade May 29 '09 at 20:48
  • 1
    Python will not automatically call the superclass' __init__ function when instantiating a subclass. This responsability is left to the programmer. I don't know if there's a rationale behind this. – Martin Cote May 29 '09 at 21:32
5

Employee has to explicitly invoke the parent's __init__ (not init):

 class Employee(Person):  
    def __init__(self):  
         Person.__init__(self)  
         self.empnum = 'abc123'  
wr.
  • 2,841
  • 1
  • 23
  • 27
2

Instead of super(class, instance) pattern why not just use super(instance) as the class is always instance.__class__?

Are there specific cases where it would not be instance.__class__?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    wow. I think it erased part of my comment. it should be "instance.__class__" of course. – Samantha Atkis Nov 10 '10 at 18:26
  • This is actually an excellent question. Please watch http://pyvideo.org/video/879/the-art-of-subclassing (TL;DW: no, it's not `instance.__class__`. "self" is not neccessarily you.) – Veky May 09 '14 at 08:38