Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does object's __init__() method do in python? [duplicate]

Tags:

While reading the code of OpenStack and I encountered this.

A class named 'Service' inherits the base class 'object', and then in Service's __init__() method, object's __init__ is called. The related code looks like this:

the class definition:

class Service(object): 

and Service's init method definition:

def __init__(self, host, binary, topic, manager, report_interval=None,              periodic_interval=None, *args, **kwargs): 

and a call to super(the 'object' here) in Service's init:

super(Service, self).__init__(*args, **kwargs) 

I don't understand last call, object.__init__() what does it actually do? can anyone help?

like image 947
can. Avatar asked Dec 23 '11 02:12

can.


People also ask

What is the __ str __ method in Python?

Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object. This method must return the String object.

What is the purpose of the __ init __ method?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

What does an __ init __ method return?

__init__ doesn't return anything and should always return None .

How does __ add __ work?

__add__ magic method is used to add the attributes of the class instance. For example, let's say object1 is an instance of a class A and object2 is an instance of class B and both of these classes have an attribute called 'a', that holds an integer.


2 Answers

The short answer is that object.__init__() method does nothing except check that no arguments have been passed in. See the source for details.

When called on an instance of Service, the super() call will delegate to object.__init__() and nothing will happen.

However, when called on an instance of a subclass of Service, things get more interesting. The super() call can potentially delegate to some class other than object, a class that is a parent of the instance but not a parent of Service. For details on how this works and why it is useful, see the blog post Python's Super Considered Super!

The following example (somewhat contrived) shows how a subclass of Service can cause the super call in Service to be directed to another class called Color:

class Service(object):     def __init__(self, host, binary, topic, manager, report_interval=None,              periodic_interval=None, *args, **kwargs):         print 'Initializing Service'         super(Service, self).__init__(*args, **kwargs)  class Color(object):     def __init__(self, color='red', **kwargs):         print 'Initializing Color'         self.color = color         super(Color, self).__init__(**kwargs)  class ColoredService(Service, Color):     def __init__(self, *args, **kwds):         print 'Initializing Colored Service'         super(ColoredService, self).__init__(*args, **kwds)  c = ColoredService('host', 'bin', 'top', 'mgr', 'ivl', color='blue') 

In the example, initializations occur in the following order:

  1. Initializing Colored Service
  2. Initializing Service
  3. Initializing Color
  4. Initialize object -- doing nothing except argument checking
like image 149
Raymond Hettinger Avatar answered Nov 02 '22 10:11

Raymond Hettinger


super() does not always return a proxy for the parent class. Instead, it returns a proxy for the next class in MRO. In single-inheritance there is no difference between MRO and the inheritance chain. In multiple-inheritance, MRO may result in a class on the other inheritance chain instead.

like image 24
Ignacio Vazquez-Abrams Avatar answered Nov 02 '22 10:11

Ignacio Vazquez-Abrams