By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Calling init function of Parent Class

P: 20
Let's say I have a class A which inherits from B. If I need to call the init function of B, I could just say super(A,self).__init__() in the init function of A.

I was wondering how do I call the init function of B from class C, where I instantiate A.

Usual case would be
Expand|Select|Wrap|Line Numbers
  1. Class A(b.B): 
  2.     def __init__(self):
  3.         super(A,self).__init__() 
  4.  
What I wanted was

Expand|Select|Wrap|Line Numbers
  1. Class A(b.B):
  2.     def __init__(self):
  3.  
  4.  
  5. Class C:
  6.      def __init__(self):
  7.           a_obj = a.A() 
  8.           and now I want to call the init of B
  9.  
Thanks

Is this possible ?
May 30 '07 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
Let's say I have a class A which inherits from B. If I need to call the init function of B, I could just say super(A,self).__init__() in the init function of A.

I was wondering how do I call the init function of B from class C, where I instantiate A.

Usual case would be
Expand|Select|Wrap|Line Numbers
  1. Class A(b.B): 
  2.     def __init__(self):
  3.         super(A,self).__init__() 
  4.  
What I wanted was

Expand|Select|Wrap|Line Numbers
  1. Class A(b.B):
  2.     def __init__(self):
  3.  
  4.  
  5. Class C:
  6.      def __init__(self):
  7.           a_obj = a.A() 
  8.           and now I want to call the init of B
  9.  
Thanks

Is this possible ?
Method overrides are seached in a chain and the FIRST match found is the one that gets called. Search rules are slightly different for new-style classes which inherit for object (which you must use because to the super() syntax that you use). So:
If A inherits from B AND has an __init__(), A's __init__() will be called. If A does not have that method, B's will be called.

Hope that helps.
May 30 '07 #2

P: 20
Method overrides are seached in a chain and the FIRST match found is the one that gets called. Search rules are slightly different for new-style classes which inherit for object (which you must use because to the super() syntax that you use). So:
If A inherits from B AND has an __init__(), A's __init__() will be called. If A does not have that method, B's will be called.

Hope that helps.
I understand that the first init will be called and to call the init of Parent we need to explicitly say it using super. To make things more clearer, lets say A is user script and I don't want him to call super to call init of his parent. Can I do this somewhere in the background when I instantiate A.
May 30 '07 #3

bartonc
Expert 5K+
P: 6,596
I understand that the first init will be called and to call the init of Parent we need to explicitly say it using super. To make things more clearer, lets say A is user script and I don't want him to call super to call init of his parent. Can I do this somewhere in the background when I instantiate A.
It seems that new-style objects behave just as you want. I tried to call SuperClass's __inti__(), but couldn't do it.
Expand|Select|Wrap|Line Numbers
  1. class SuperClass(object):
  2.     def __init__(self):
  3.         print "initing SuperClass"
  4.  
  5. class SubClassOne(SuperClass):
  6.     def __init__(self):
  7.         print "initing SubClassOne"
  8. class SubClassFour(SubClassOne):
  9.     def __init__(self):
  10.         print "initing SubClassFour"
  11.         SuperClass.__init__(self)
  12.  
  13. inst4 = SubClassFour()
  14.  
initing SubClassFour
initing SuperClass


I have read somewhere that there is a way to prevent case #4. I'll look around and see if I can re-discover that tid-bit of class instantiation control.
May 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.