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

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

Leave a Comment