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
+ 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 Class A(b.B):      def __init__(self):         super(A,self).__init__()    What I wanted was Expand|Select|Wrap|Line Numbers Class A(b.B):     def __init__(self):     Class C:      def __init__(self):           a_obj = a.A()            and now I want to call the init of B   Thanks Is this possible ? May 30 '07 #1
Share this Question
3 Replies

 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 Class A(b.B):      def __init__(self):         super(A,self).__init__()    What I wanted was Expand|Select|Wrap|Line Numbers Class A(b.B):     def __init__(self):     Class C:      def __init__(self):           a_obj = a.A()            and now I want to call the init of B   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

 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 class SuperClass(object):     def __init__(self):         print "initing SuperClass"   class SubClassOne(SuperClass):     def __init__(self):         print "initing SubClassOne" class SubClassFour(SubClassOne):     def __init__(self):         print "initing SubClassFour"         SuperClass.__init__(self)   inst4 = SubClassFour()   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.