5

I am learning python via dive into python. Got few questions and unable to understand, even through the documentation.

1) BaseClass

2) InheritClass

What exactly happens when we assign a InheritClass instance to a variable, when the InheritClass doesn't contain an __init__ method and BaseClass does ?

  • Is the BaseClass __init__ method called automatically
  • Also, tell me other things that happen under the hood.

Actually the fileInfo.py example is giving me serious headache, i am just unable to understand as to how the things are working. Following

detly
  • 29,332
  • 18
  • 93
  • 152
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104

2 Answers2

6

Yes, BaseClass.__init__ will be called automatically. Same goes for any other methods defined in the parent class but not the child class. Observe:

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

The child inherits its parent's methods. It can override them, but it doesn't have to.

FogleBird
  • 74,300
  • 25
  • 125
  • 131
  • You meant to say, every method in the `BaseClass` will be called, irrespective of calling them or not ?? – Pankaj Upadhyay Jul 26 '11 at 14:44
  • 2
    No, `child.func` will call `func` in the parent class if it does not exist in the child class. – FogleBird Jul 26 '11 at 14:45
  • 2
    The point is that on creating `Child`, there is a implicit call to the `__init__` method of `Child`, which is inherited whenever it is not explicitly defined. – Eric Wilson Jul 26 '11 at 14:49
  • @Farmboy, @FogleBird : Thanks !! Can we use the value derived from `BaseClass.__init__` method in the `InheritClass.methods` ?? – Pankaj Upadhyay Jul 26 '11 at 14:56
  • 2
    If you want to call `Parent.__init__` in the `Child.__init__` method, you need to pass the child instance `self` as follows: `class Child(Parent): def __init__(self): Parent.__init__(self) #other child stuff` – Eric Wilson Jul 26 '11 at 15:04
  • 1
    In general if you have a `child.func` method, but you want to call the `parent.func` method, with an instance `c` of `Child`, then you need to call `func` as an unbound method, explicitly passing in the instance: `Parent.func(c)`. Normally, when you call `func` as a bound method to an instance `c` as `c.func()` this is converted to `Child.func(c)`, where `c` is the instance `self` in the method definition. If this seems confusing, keep in mind that typically overriding an inherited method is indication that the child class will not want access to the parents method. – Eric Wilson Jul 26 '11 at 15:10
  • 1
    The `__init__` method is the most common exception to this, where you are likely to want the setup accomplished in the parents `__init__` along with some custom setup for the child. – Eric Wilson Jul 26 '11 at 15:13
  • It would improve the readability of this discussion to move the explanation into the actual answer. – SingleNegationElimination Jul 26 '11 at 15:33
4

@FogleBird has already answered your question, but I wanted to add something and can't comment on his post:

You may also want to look at the super function. It's a way to call a parent's method from inside a child. It's helpful when you want to extend a method, for example:

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)

This can of course encompass methods that are a lot more complicated, not the __init__ method or even a method by the same name!

bdeniker
  • 995
  • 1
  • 9
  • 21