Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Class Inheritance issue

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()
like image 536
meade Avatar asked May 29 '09 20:05

meade


People also ask

Why inheritance is not working in Python?

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

Does Python support class inheritance?

Inheritance is a required feature of every object oriented programming language. This means that Python supports inheritance, and as you'll see later, it's one of the few languages that supports multiple inheritance.


2 Answers

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.

like image 192
workmad3 Avatar answered Oct 03 '22 18:10

workmad3


You should explicitely call the superclass' init function:

class Employee(Person):
    def __init__(self):
        Person.__init__(self)
        self.empnum = "abc123"
like image 34
Martin Cote Avatar answered Oct 03 '22 19:10

Martin Cote