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
30.6k20 gold badges101 silver badges161 bronze badges
asked Jul 26, 2011 at 14:32

2 Answers 2

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.

answered Jul 26, 2011 at 14:41
Sign up to request clarification or add additional context in comments.

8 Comments

No, child.func will call func in the parent class if it does not exist in the child class.
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.
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
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.
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.
|
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!

answered Jul 26, 2011 at 15:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.