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

Cans omeone help me explaining what am I doing wrong here? I`m getting an error like:

B.__init__()
TypeError: unbound method __init__() must be called with B instance as first argument (got nothing instead)

Reading from google,i guess,its not good to call __init__ explicit,but i want to print when an object is created,so i can do that only by creating an object inside another object? like composition in c++ ?

Recommended Answers

All 3 Replies

I copied your code exactly - yet failed to recreate your problem.

However whenever I used:

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__()
        print 'C()'


c = C()

I got your error.

File "test.py", line 16, in <module>
    c = C()
  File "test.py", line 12, in __init__
    B.__init__()
TypeError: unbound method __init__() must be called with B instance as first argument (got nothing instead)

Seems you solved your own problem when posting the code :p.

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

Well,this was a strange post, I still don`t know what happend. Thanks for replyes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.