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

Diez B. Roggisch deets at nospam.web.de
Wed Mar 14 12:48:02 EDT 2007


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



More information about the Python-list mailing list