Problem with inheritance

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Oct 21 05:49:49 EDT 2011


Sverre wrote:
> I have to classes a  and b
>
>
> class a(object):
>     def __init__(self,x):
>         self.x = x
>         self.build()
>
>     def build(self):
>         return
>
> class b(a):
>     def __init__(self,x):
>         a.__init__(self,x)
>         self.y = 0  # ???
>
>     def build(self):
>         # do something
>         self.y += 2*self.x
>
>  t = b(1)
>
> The line marked with "???" will no be executed and I don't know the
> reason. This example is working as intended, but not not the code I'm
> working on. I'm using Eclipse. I don't know how to debug this
> problem.
>
>   

By the way, you're executing self.y += 2*self.x before initializing it to 0.

class b(a):
    def __init__(self,x):
        self.y = 0  
        a.__init__(self,x)


Note that having the constructor of 'a' calling an overriden method by 
'b' (build) is kinda funny. I would advise not to do so unless required.

JM



More information about the Python-list mailing list