overloading methods python

Solutions on MaxInterview for overloading methods python by the best coders in the world

showing results for - "overloading methods python"
Iker
19 Feb 2016
1class A():
2  def __init__(self, name):
3    self.name = name
4  
5  # This func will define what happens when adding two obects type A
6  def __add__(self, b):
7    return (str(self.name) + " " + str(b.name))
8  
9  # This func will define what happens when converting object to str
10  def __str__(self):	# Requested with the print() func
11    return self.name
12
13Var1 = A('Gabriel')
14Var2 = A('Stone')
15
16Var3 = Var1 + Var2
17
18print(Var1)
19print(Var2)
20print(Var3)