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.
- Types of Methods in a Class
- Instance Methods in Python
- Setter and Getter methods in Python
- Class Methods in Python
- Static Methods in Python
- Nested Classes in Python
- 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.
- Instance Methods
- Class Methods
- 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()
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.
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)
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.
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())
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.
- Class methods should be declared with @classmethod.
- 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.
- 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())
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.
- We can declare static method explicitly by using @staticmethod decorator.
- 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)
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()
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:
- In old languages like C++, programmers are responsible for both creation and destruction of objects.
- Usually a programmer should take very much care to create objects, and sometimes he may neglect destruction of useless objects.
- 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.
- But in Python, internally a program will run in background always to destroy useless objects.
- So, the chance of failing a Python program with memory problems are very little.
- This program is nothing but Garbage Collector.
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.
- gc.isenabled() – This method returns True if garbage collector is enabled
- gc.disable() – This function is used to is disable the garbage collector explicitly
- 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())
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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.