Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a basic example of single inheritance using the super() keyword in Python?

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?

like image 538
lfaraone Avatar asked Jul 23 '09 19:07

lfaraone


People also ask

What is super keyword in Python with example?

Example 1: super() with Single InheritanceIn the case of single inheritance, we use super() to refer to the base class. Dog has four legs. Dog is a warm-blooded animal. Since we do not need to specify the name of the base class when we call its members, we can easily change the base class name (if we need to).

What is the use the super () function in Python inheritance?

The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What is single inheritance in Python?

In python single inheritance, a derived class is derived only from a single parent class and allows the class to derive behaviour and properties from a single base class. This enables code reusability of a parent class, and adding new features to a class makes code more readable, elegant and less redundant.

What is the super () method used for?

The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.


2 Answers

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 
like image 149
ire_and_curses Avatar answered Oct 02 '22 18:10

ire_and_curses


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.)

like image 37
JAB Avatar answered Oct 02 '22 18:10

JAB