Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object

I have the following Python 2.7 code:

class Frame:     def __init__(self, image):         self.image = image  class Eye(Frame):     def __init__(self, image):         super(Eye, self).__init__()         self.some_other_defined_stuff() 

I'm trying to extend the __init__() method so that when I instantiate an 'Eye' it does a bunch of other stuff (self.some_other_defined_stuff()), in addition to what Frame sets up. Frame.__init__() needs to run first.

I get the following error:

super(Eye, self).__init__() TypeError: must be type, not classobj 

Which I do not understand the logical cause of. Can someone explain please? I'm used to just typing 'super' in ruby.

like image 884
cjm2671 Avatar asked Apr 16 '14 18:04

cjm2671


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.

What does super () __ Init__ do in Python?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.

How does Super work with multiple inheritance Python?

Python super() function with multiple inheritance Python supports multiple inheritance, in which the derived class can inherit from multiple base classes that don't necessarily inherit from each other. In multiple inheritance, the features of all the parent classes are inherited into the child class.

How does super () method play an important role in inheritance?

In a class hierarchy with single inheritance, super helps to refer to the parent classes without naming them explicitly, thus making the code more maintainable. Here, with the help of super(). f1(), the f1() method of the super class of Child class i.e Parent class has been called without explicitly naming it.


2 Answers

There are two errors here:

  1. super() only works for new-style classes; use object as a base class for Frame to make it use new-style semantics.

  2. You still need to call the overridden method with the right arguments; pass in image to the __init__ call.

So the correct code would be:

class Frame(object):     def __init__(self, image):         self.image = image  class Eye(Frame):     def __init__(self, image):         super(Eye, self).__init__(image)         self.some_other_defined_stuff() 
like image 151
Martijn Pieters Avatar answered Sep 21 '22 07:09

Martijn Pieters


Frame must extend object because only the new style classes support super call you make in Eye like so:

class Frame(object):     def __init__(self, image):         self.image = image  class Eye(Frame):     def __init__(self, image):         super(Eye, self).__init__(image)         self.some_other_defined_stuff() 
like image 29
myusuf3 Avatar answered Sep 23 '22 07:09

myusuf3