Python Class Methods Tutorial

Python class is used to put data and methods into a single structure. By using the classes new types are created and the methods are used to take different actions for internal and external data. Methods are one of the most powerful features of Python classes. In this tutorial, we examine how to create, overload, and use the Python class methods.

Create Class

Let’s start by creating a new simple class in order to work on the methods. In the following example, we create a simple class that only has two variables named name and age.

class Person:
   name="İsmail Baydan"
   age=38

Create Class Method

In the step, we create a class method that simply returns the class variable’s values. The method is named as print() . As the data is provided via the class we provide the self parameter to the print() method.

class Person:
   name="İsmail Baydan"
   age=38
   def print(self):
      print(self.name)
      print(self.age)

Person p = Person()

p.print()

Overload Class Method

In some cases, we may need to implement the same method multiple times with different parameters. This is called as method overload . We can define a method multiple times using different parameters. In the following example, we overload the method named calculate() .

class Person:
   name="İsmail Baydan"
   age=38
   def print(self):
      print(self.name)
      print(self.age)
   def calculate(a,b):
      return a+b
   def calculate(a,b,c):
      return a+b+c
   def calculate(a,b,c,d):
      return a+b+c+d

p = Person()

x = p.calculate(1,2)

y = p.calculate(1,2,3)

z = p.calculate(1,2,3,4)

Create Constructor Method __init__()

One of the most useful class method is the __init__() method that is used to construct the class. We provide some values to the class constructor and the __init__() method is used to set these values for the newly initialized object.

class Person:
   name=""
   age=-1
   def __init__(self):
      self.name="İsmail Baydan"
      self.age=38

Overload Constructor Method __init__()

The __init__() method can be used multiple times for a different count of parameters. This is called constructor overload. In the following example, we create 2 __init__() methods for different counts of parameters.

class Person:
   name=""
   age=-1
   def __init__(self,name):
      self.name=name
      self.age=38
   def __init__(self,name,age):
      self.name=name
      self.age=age

p1 = new Person("İsmail Baydan")

p2 = new Person("Ahmet Ali Baydan",9)

Create String __str__() Method

Python is a dynamic language that can be used to print everything like objects, and instances. We can also print an instance or object. The __str__() method can be used to set string representation of the class or object.

class Person:
   name="İsmail Baydan"
   age=38
   def __str__(self):
      return self.name+" "+self.age

p = Person()

print(p)

Add Class Method Dynamically

We can add a method into the existing class dynamically outside of the class definition. The classmethod() the method can be used to add a new method to the existing class.

class Person:
   name="İsmail Baydan"
   age=38


def calculate(a,b):
   return a+b

Person.calculate=classmethod(calculate)

p = Person()

a=p.calculate(1,2)

Leave a Comment