Python Forum

Full Version: parent/add and child/div
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
can i use my parent class addition answer in child class to do division and how ?
class a:
    def __init__(self,x):
        self.x=x
    def __str__(self):
        return 'addition is(%d)'%(self.x)
    def __add__(self,other):
        c=self.x+other.x
        return a(self.x+other.x)

a1=a(2)
a2=a(5)
c=a1+a2
print(c)

class b(a):
Same way you would with any other method: by calling it.
class b(a):
    def spam(self, other):
        return self + other