Define class with methods and invoke them in python

class Person:
   def __init__(self, first, middle, last, age):
      self.first = first;
      self.middle = middle;
      self.last = last;
      self.age = age;
 
   def __str__(self):
      return self.first + ' ' + self.middle + ' ' + self.last + \
        ' ' + str(self.age)
 
   def initials(self):
      return self.first[0] + self.middle[0] + self.last[0]
 
   def changeAge(self, val):
      self.age += val
 
myPerson = Person('Raja', 'I', 'Kumar', 21)
print(myPerson)
myPerson.changeAge(5)
print(myPerson)
print(myPerson.initials())

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.