(beginners) howto ascribe _all_ fields of parent class to child class?

Discussion in 'Python' started by dmitrey, Mar 14, 2007.

  1. dmitrey

    dmitrey Guest

    Hi all,
    I'm rewriting some code from other language to Python; can anyone
    explain me which way is the simpliest:
    I have
    class C1():
    def __init__(self):
    self.a = 5

    class C2(C1):
    def __init__(self):
    self.b = 8

    c = C2()
    print c.b#prints 8
    print c.a#prints error, because field a is absent

    so how can I wrote the code that I'll got all class C1 fields (not
    only funcs)

    Thank you in advance,
    Dmitrey
    dmitrey, Mar 14, 2007
    #1
    1. Advertising

  2. Re: (beginners) howto ascribe _all_ fields of parent class to childclass?

    dmitrey schrieb:
    > Hi all,
    > I'm rewriting some code from other language to Python; can anyone
    > explain me which way is the simpliest:
    > I have
    > class C1():
    > def __init__(self):
    > self.a = 5
    >
    > class C2(C1):
    > def __init__(self):
    > self.b = 8
    >
    > c = C2()
    > print c.b#prints 8
    > print c.a#prints error, because field a is absent
    >
    > so how can I wrote the code that I'll got all class C1 fields (not
    > only funcs)


    You need to call the super classes __init__-method. There are several
    ways to do so, in your case

    class C2(C1):
    def __init__(self):
    C1.__init__(self)
    self.b = 8

    should do the trick.

    DIEZ
    Diez B. Roggisch, Mar 14, 2007
    #2
    1. Advertising

  3. dmitrey

    Steve Holden Guest

    Re: (beginners) howto ascribe _all_ fields of parent class to childclass?

    dmitrey wrote:
    > Hi all,
    > I'm rewriting some code from other language to Python; can anyone
    > explain me which way is the simpliest:
    > I have
    > class C1():
    > def __init__(self):
    > self.a = 5
    >
    > class C2(C1):
    > def __init__(self):
    > self.b = 8
    >
    > c = C2()
    > print c.b#prints 8
    > print c.a#prints error, because field a is absent
    >
    > so how can I wrote the code that I'll got all class C1 fields (not
    > only funcs)
    >
    > Thank you in advance,
    > Dmitrey
    >


    The only problem here is that your subclass doesn't call the __init__
    method of the superclass. Rewrite your C2 definition to read

    class C2(C1):
    def __init__(self):
    C1.__init__(self)
    self.b = 8

    and you should find your example works as you want.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
    Steve Holden, Mar 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xif
    Replies:
    3
    Views:
    308
    Nick Coghlan
    Mar 1, 2005
  2. Steven Bethard
    Replies:
    2
    Views:
    269
    Raymond Hettinger
    Mar 2, 2005
  3. metal
    Replies:
    8
    Views:
    501
    Bruno Desthuilliers
    Oct 30, 2009
  4. Noel Dolan
    Replies:
    0
    Views:
    227
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    130
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page