Problem with inheritance

S

Sverre

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.
 
S

Sverre

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.

I found the solution. I caused an exception in b.build that wasn't
reported by Eclipse properly.
 
C

Chris Angelico

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.

Did you notice the error you got when you tried? Copying and pasting
your example into IDLE shows this (Python 3):
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
t=b(1)
File "<pyshell#77>", line 3, in __init__
a.__init__(self,x)
File "<pyshell#76>", line 4, in __init__
self.build()
File "<pyshell#77>", line 8, in build
self.y += 2*self.x
AttributeError: 'b' object has no attribute 'y'

When a calls self.build(), it calls b's build() function. That tries
to modify self.y, which then fails. If you put the self.y = 0 line
above the chaining call to a.__init__, all is well.

Incidentally, you may wish to replace the a.__init__ call with this:
super().__init__(x)

That way, you're not repeating the name 'a', and if you change the
inheritance tree, you don't need to change your code.

ChrisA
 
J

Jean-Michel Pichavant

Sverre said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top