Kam Kam - 1 year ago 67
Python Question

using inheritance and calling in other class


class aa:
def getDis():
return 0

class bb(aa):
def getDis():
return 10

class cc(aa):
def getDis():
return 15

class d:
def __init__(self,cust):
self.cust = cust
self.memberLevel = aa()

def cgl(self,amt):
if amt ==1:
self.memberLevel = bb()
elif amt ==2:
self.memberLevel = cc()
self.memberLevel = aa()

Well i have these four classes. What i am trying to do is that in my top three classes i have inheritance. In my class d i am trying to call the respective method if the amount is 1 0r 2 . Instead it is giving me this output:

d1 = d('one')


<__main__.bb object at 0x036D7BF0>

So help required that how can i use overriding technique.

Answer Source

It seems you were hoping to see the console output:10

Because d1.memberLevel is the bb class itself (not the value 10, and not the function that returns the value 10), the console output shows <__main__.bb object at 0x036D7BF0>. This is correct behavior when printing an object directly.

Perhaps your last line of console code should say:


Another option would be to assign the value within the "d" class:

self.memberLevel = bb().getDis()

And the member methods require "self", e.g.:

def getDis(self):
    return 10
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download