4

I am trying to call the __init__() method in a superclass, where said method takes arguments, but it doesn't seem to be working. Please see the code below:

>>> class A:
        def __init__(self, param1, param2):
            self._var1 = param1
            self._var2 = param2

>>> class B(A):
        def __init__(self, param1, param2, param3):
            super(B, self).__init__(param1, param2)
            self._var3 = param3


>>> a = A("Hi", "Bob")
>>> a._var1
'Hi'
>>> a._var2
'Bob'
>>> 
>>> b = B("Hello", "There", "Bob")

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    b = B("Hello", "There", "Bob")
  File "<pyshell#69>", line 3, in __init__
    super(B, self).__init__(param1, param2)
TypeError: must be type, not classobj
>>>

I have never been able to get this to work. What am I doing wrong? I would ideally like to use super() over A.__init__(self, <parameters>), if this is a possibility (which it must be).

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Lord Cat
  • 401
  • 4
  • 13

3 Answers3

5

As a rule of thumb: in Python 2 your base class should always inherit from object, as otherwise you're using old style classes with surprising behaviors like the one you describe.

So try

class A(object):
    ...

like this:

In [1]: class A(object):
   ...:     def __init__(self, param1, param2):
   ...:         self._var1 = param1
   ...:         self._var2 = param2
   ...:

In [2]: class B(A):
   ...:     def __init__(self, param1, param2, param3):
   ...:         super(B, self).__init__(param1, param2)
   ...:         self._var3 = param3
   ...:

In [3]: a = A("Hi", "Bob")

In [4]: a._var1
Out[4]: 'Hi'

In [5]: a._var2
Out[5]: 'Bob'

In [6]: b = B("Hello", "There", "Bob")

In [7]: b._var3
Out[7]: 'Bob'

If you really know what you're doing (at least see the docs and this) and you want to use old-style classes you can't use super(), but instead need to manually call the super-class's __init__ from the sub class like this:

class A:
    ...

class B(A):
    def __init__(self, p1, p2, p3):
        A.__init__(p1, p2)
        ...
Community
  • 1
  • 1
Jörn Hees
  • 3,338
  • 22
  • 44
3

You can still use old style classes but in this case base class __init__ method should be called explicitly:

class A:
    def __init__(self, param1, param2):
        self._var1 = param1
        self._var2 = param2

class B(A):
    def __init__(self, param1, param2, param3):
        A.__init__(self, param1, param2)
        self._var3 = param3

Test:

>>> b = B("Hello", "There", "Bob")
>>> b._var1
'Hello'
>>> b._var2
'There'
>>> b._var3
'Bob'
>>>
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • 2
    In general, you shouldn't be writing old-style classes in new code anymore, but this is good to know because even in the standard library, there are still instances of old-style classes. – chepner May 06 '15 at 13:08
1

The fix is to reference object class when declaring A

class A(object):
    def __init__(self, param1, param2):
        self._var1 = param1
        self._var2 = param2

This allows the script to execute successfully.

Astr-o
  • 505
  • 4
  • 10