In [95]:
class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B():
def __init__(self, d, e, A):
self.d = d
self.e = e
self.A = A
def addAllNums(self):
x = self.d + self.e + self.A.b + self.A.c
return x
objA = A("hi", 2, 6)
objB = B(5, 9, objA)
objB.addAllNums()
Out[95]:
In [91]:
class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B():
def __init__(self, d, e):
self.d = d
self.e = e
def addAllNums(self, arg):
x = self.d + self.e + arg.b + arg.c
return x
objA = A("hi", 2, 6)
objB = B(5, 9)
objB.addAllNums(objA)
Out[91]:
In [94]:
class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B():
def __init__(self, d, e):
self.d = d
self.e = e
self.objA = A("hi", 2, 6)
def addAllNums(self):
x = self.d + self.e + self.objA.b + self.objA.c
return x
objB = B(5, 9)
objB.addAllNums()
Out[94]:
In [102]:
class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B(A):
def __init__(self, a, b, c, d, e):
# A.__init__(self, a, b, c)
super().__init__(a, b, c)
self.d = d
self.e = e
def addAllNums(self):
x = self.a + self.b + self.c + self.d + self.e
return x
objB = B(1, 2, 3, 5, 9)
objB.addAllNums()
Out[102]:
In [111]:
class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums(self):
return self.b * self.c
class B(A):
def __init__(self, a, b, c, d, e):
super().__init__(a, b, c)
self.d = d
self.e = e
def addNums(self):
return self.d + self.e
def check(self):
print("Class B func:", self.addNums())
print("Class B func:", super().addNums())
objB = B(1,2,3,5, 9)
objB.check()
In [116]:
class A():
def f1(self, x):
return x
def f1(self, x, y):
return x, y
objA = A()
objA.f1(8,5)
# objA.f1(8) # Gives error
Out[116]:
In [121]:
# How to work with function overloading
class A():
def f1(self,name = None):
if name is None:
return 5
else:
return name
objA = A()
print(objA.f1())
objA.f1(8)
Out[121]: