LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2011, 07:47 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
inheritance in python


Hello i want to create a 3 level hierarchy with constructor that print something and create a most derived object.

Code:
class A:
    def __init__(self):
        print 'A()'
        
class B(A):
    def __init__(self):
        A.__init__(self)
        print 'B()'
        
class C(B):
    def __init__(self):
        B.__init__(self)
        print 'C()'
        
c = C()
But i am getting an error:
Code:
 B.__init__()
TypeError: unbound method __init__() must be called with B instance as first argument (got nothing instead)
 
Old 01-21-2011, 10:42 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Your code works fine for me with Python 2.6.6

What version of python are you using?
 
Old 01-21-2011, 10:48 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,515

Rep: Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872
Code:
class A(object):
    def __init__(self):
        print 'A()'
        
class B(A):
    def __init__(self):     
        super(B, self).__init__()
        print 'B()'
        
class C(B):
    def __init__(self):
        super(C, self).__init__()
        print 'C()'
Try this instead.
 
Old 01-21-2011, 11:01 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Hm, I understand why your code worked, but now I am puzzled as to why it worked on my system at all... I also ran it under 3.1.2 and it worked fine - was something changed to allow the syntax which the OP originally used at some point in Python's development? I can't find any reference to it on the web...

Thanks,
 
Old 01-21-2011, 11:10 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,515

Rep: Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872
It's the opposite. His syntax fell out of flavour.

He's using old-style classes.
 
Old 01-21-2011, 01:56 PM   #6
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
After posting here,it got working.Same code. I am confuse why i got error in the first place and why now is working.I am using 2.7
 
Old 01-22-2011, 04:16 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by dugan View Post
It's the opposite. His syntax fell out of flavour.

He's using old-style classes.
So why does it work in 2.6.6 and 3.1.2 but not 2.7? Or am I missing something?

Quote:
I am confuse why i got error in the first place and why now is working.I am using 2.7
As far as I understand it (and dugan may be able to improve on this explanation), you have set up inheritances but each class has its own __init__ method, so when you try to call __init__ to run the parent's __init__ code the method has already been overwritten. http://timvalenta.wordpress.com/2009...pythons-super/
 
Old 01-22-2011, 06:08 AM   #8
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
So why does it work in 2.6.6 and 3.1.2 but not 2.7? Or am I missing something?
I'm not sure what the OP was originally doing but the posted code works fine for me in python 2.6.4,python 2.7 and with the appropriate changes to the print statement python 3.1.2. I know the recomended way of doing this is now to call super() but as far as I know the "old" way of calling an unbound method should work anyway. James Knight warns about using super here although I always use it and haven't noticed the drawbacks he mentions - I think that article is now rather old. The point is I am sure support for calling the superclass's initialiser as an unbound method should still work. Not being derogatory here but possibly vbx_wx missed or mispelt a "self" somewhere originally - it can happen to anyone.
 
Old 01-22-2011, 12:33 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,515

Rep: Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872
Quote:
Originally Posted by Snark1994 View Post
So why does it work in 2.6.6 and 3.1.2 but not 2.7? Or am I missing something?
You're missing the fact that the code he posted was obviously not what he tried to run.

What he posted works fine in 2.7.
 
Old 01-23-2011, 07:57 AM   #10
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Ah right, thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
Python inheritance and scope dakensta Programming 2 07-31-2005 06:16 AM
python inheritance towlie Programming 3 09-23-2004 12:13 AM
inheritance kalleanka Programming 4 02-29-2004 07:19 PM
OOP / Python inheritance question usernamenumber Programming 1 12-02-2003 10:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration