Python classes and __init__ method

I am learning python through immersion in python. I got a few questions and could not understand even through the documentation.

1) BaseClass

2) InheritClass

What exactly happens when we assign an instance of InheritClass to a variable, when InheritClass does not contain a method __init__, and BaseClass does?

  • The BaseClass method is called __init__automatically
  • Also tell me what's going on under the hood.

Actually the fileInfo.py example gives me a serious headache, I just can not understand how this works. Following

+5
source share
2 answers

, BaseClass.__init__ . , , . :

>>> class Parent(object):
...   def __init__(self):
...     print 'Parent.__init__'
...   def func(self, x):
...     print x
...
>>> class Child(Parent):
...   pass
...
>>> x = Child()
Parent.__init__
>>> x.func(1)
1

. , .

+6

@FlogBird , - :

super . . , , :

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

class ChildClass(ParentClass):
    def __init__(self, x, y):
        self.y = y
        super(ChildClass, self).__init__(x)

, , , __init__ !

+4

All Articles