Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 2, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/83e8293d76eb8f8cc1584d96f3e143b6 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/83e8293d76eb8f8cc1584d96f3e143b6 to your computer and use it in GitHub Desktop.
class Student:
def __init__(self,name,rollno,age):
self.name=name
self.rollno=rollno
self.age=age
def __str__(self):
return "{}-{}-{}".format(self.name,self.rollno,self.age)
def __add__(self,other):
return (self.age+other.age)
s1=Student("karthi",12,7)
s2=Student("Sarvesh",15,3)
#when trying to add two class objects,magic method __add__() is invoked.
#It will add age attribute of two Student objects according to the behavior mentioned in the method __add__()
print (s1+s2)#Output: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment