Types of Methods in a Class in Python

Types of Class Methods in Python

In this article, I am going to discuss Types of Class Methods in Python with examples. Please read our previous article where we discussed Types of Class Variables in Python. As part of this article, we are going to discuss the following pointers which are related to Class Methods in Python.

  1. Types of Methods in a Class
  2. Instance Methods in Python
  3. Setter and Getter methods in Python
  4. Class Methods in Python
  5. Static Methods in Python
  6. Nested Classes in Python
  7. Garbage Collection in Python
Types of Methods in a Class

In python, we can classify the methods into three types from the perspective of object oriented programming.

  1. Instance Methods
  2. Class Methods
  3. Static Methods
Instance Methods in Python:

Instance methods are methods which act upon the instance variables of the class. They are bound with instances or objects, that”s why called as instance methods. The first parameter for instance methods should be self variable which refers to instance. Along with the self variable it can contain other variables as well.

Example: Instance Methods in Python (demo29.py)

class Demo:
   def __init__(self, a):
       self.a=a
   def m(self):
       print(self.a)
d=Demo(10)
d.m()

Output: Instance Methods in Python

Setter and Getter methods in Python:

Setter methods can be used to set values to the instance variables. They are also known as mutator methods. Their main aim is to only set values to instance variables, hence they don’t return anything.

Syntax:

Setter and Getter methods in Python

Example: Setter Methods in Python (demo30.py)

class Customer:
   def set_name(self, name):
       self.name=name
   def set_id(self, id):
       self.id=id
c=Customer()
c.set_name("Balayya")
c.set_id(1)

print(c.name)

Output:

Setter and Getter methods in Python

Getter methods are used to get the values of instance variables. They return the value in the instance variable. Getter methods also known as accessor methods.

Syntax:

Types of Class Methods in Python

Example: Getter Methods in Python (demo31.py)

class Customer:
   def set_name(self, name):
       self.name=name
   def set_id(self, id):
       self.id=id

   def get_name(self):
       return self.name

   def get_id(self):
       return self.id

c=Customer()
c.set_name("Balayya")
c.set_id(1)

print("My name is: ", c.get_name())
print("My id is: ", c.get_id())

Output:

Types of Class Methods in Python

Class Methods in Python:

Class methods are methods which act upon the class variables or static variables of the class. We can go for class methods when we are using only class variables (static variables) within the method.

  1. Class methods should be declared with @classmethod.
  2. Just as instance methods have ‘self’ as the default first variable, class method should have ‘cls’ as the first variable. Along with the cls variable it can contain other variables as well.
  3. Class methods are rarely used in python

Example: Class Methods in Python (demo32.py)

class Pizza:
   radius=200
   @classmethod
   def get_radius(cls):
       return cls.radius
print(Pizza.get_radius())

Output: Class Methods in Python

Static Methods in Python:

The static methods, in general, utility methods. Inside these methods we won’t use any instance or class variables. No arguments like cls or self are required at the time of declaration.

  1. We can declare static method explicitly by using @staticmethod decorator.
  2. We can access static methods by using class name or object reference

Example: Static Methods in Python (demo33.py)

class Demo:
   @staticmethod
   def sum(x, y):
       print(x+y)
   @staticmethod
   def multiply(x, y):
       print(x*y)
Demo.sum(2, 3)
Demo.multiply(2,4)

Output:

Static Methods in Python

Nested Classes in Python:

A class inside another class are called nested classes.

Example: Nested Classes in Python (demo34.py)

class A:

   def __init__(self):
       print("outer class object creation")
   class B:
       def __init__(self):
           print("inner class object creation")
       def m1(self):
           print("inner class method")
a=A()
b=a.B()
b.m1()

Output:

Nested Classes in Python

So, to create inner class object, first we need to create outer class object. With the created outer class object we can create an inner class object. After creating an inner class object, we can access the members of the inner class.

Garbage Collection in Python:
  1. In old languages like C++, programmers are responsible for both creation and destruction of objects.
  2. Usually a programmer should take very much care to create objects, and sometimes he may neglect destruction of useless objects.
  3. Due to this negligence, at a certain point of time there may be a chance, total memory can be filled with useless objects which creates memory problems and total application will be down with Out of memory error.
  4. But in Python, internally a program will run in background always to destroy useless objects.
  5. So, the chance of failing a Python program with memory problems are very little.
  6. This program is nothing but Garbage Collector.
Objective:

The main objective of Garbage Collector is to destroy useless objects. If an object does not have any reference variable, then that object is eligible for Garbage Collection.

By default, the Garbage collector is enabled, but we can disable it based on our requirement. In order to do so, we need to import the gc module.

Methods:
  1. gc.isenabled() – This method returns True if garbage collector is enabled
  2. gc.disable() – This function is used to is disable the garbage collector explicitly
  3. gc.enable() – This function is used to is enable the garbage collector explicitly

Example: Garbage Collection in Python (demo35.py)

import gc
print(gc.isenabled())
gc.disable()
print(gc.isenabled())
gc.enable()
print(gc.isenabled())

Output:

Garbage Collection in Python

In the next article, I am going to discuss Inheritance in Python. Here, in this article, I try to explain Types of Class Methods in Python. I hope you enjoy this Types of Class Methods in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.