Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of super(ClassName,self)._init_() [duplicate]

Tags:

python

oop

class

I have a class that looks like this:

#!/usr/bin/env python
class Foo:
    def __init__(self, x):
        self.x = x
    def bar(self):
        self.bar1_out = self.x + 5
        self.bar2_out = self.x + 1
        return (self.bar1_out,self.bar2_out)
    def qux(self,myvalue = None):
        first, second = myvalue or self.bar()
        return first + 3, second + 6

def main():
    """docstring for main"""
    f = Foo(5)

    mbr_out1, mbr_out2 = f.bar()
    print mbr_out1, "\t", mbr_out2

    mqx_out1, mqx_out2 = f.qux()
    print mqx_out1, "\t", mqx_out2

    qout1, qout2 = f.qux((1))
    print qout1, "\t", qout2

if __name__ == '__main__':
    main()

I saw some implementation that suggest using super

    def __init__(self, x):
        super(Foo,self).__init__()
        self.x = x
    def bar(self)
        #etc.

My questions are:

  1. What's the use of super(Foo,self).__init__()
  2. How does it differ from self.x=x
  3. How can I make my code top above produce the same result by using super()
like image 300
neversaint Avatar asked Mar 31 '14 07:03

neversaint


People also ask

What does super () __ Init__ mean?

The “__init__” is a reserved method in python classes. It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. Python super() The super() function allows us to avoid using the base class name explicitly.

What is the use of super in Python?

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.

Is super init necessary?

In general it is necessary. And it's often necessary for it to be the first call in your init. It first calls the init function of the parent class ( dict ).

How do you call a super function in Python?

Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by 'super'.


1 Answers

How does it differ from self.x=x?

super() is only useful if you subclass:

class Foo(object):
    def __init__(self, x):
        self.x = x

class Bar(Foo):
    def __init__(self, x):
        super(Bar, self).__init__(x)
        self.initial_status = False

is better than setting self.x = x in Bar's __init__.

The difference is that Bar doesn't need to care about the implementation of Foo.

If you choose to change Foo in a way which sets self.x = 2 * x, then you won't have to change Bar as well (which might even sit in a difference file - failure to see this is almost guaranteed).

In your example, there is no point to use super() as you don't subclass.

like image 111
glglgl Avatar answered Oct 20 '22 04:10

glglgl