Python provides broad flexibility of OOPS concepts, but it’s underrated/unknow. Today, let’s cover the usages of a different methods available in the Python OOPS teritory.
- Instance method
- Static method
- Class method
- Abstract method
Instance method
Instance methods are very basic and easy method that we use regularly when we create Class in Python. If we want to print an instance variable or instance method we must create an object of that required class. They access the unique data, i.e. Instance methods will be able to access the data and properties unique to each instance.
class RECTANGLE:
def number\_of\_sides(self):
print(“I have 2 sides”)
- Instance methods takes self as the first argument.
- Decorator is not required for instance methods.
Static Method
Static methods are related to a class in some way, but don’t need to access any class-specific data. i.e. self , is not neccessarily the first argument of the method and It doesn’t even need to instantiate an instance
Static methods will not be able to access anything in the claas, totally self-contained/isolated mode
class RECTANGLE:
def number\_of\_sides(self):
print(“I have 2 sides”)
@staticmethod
def info():
print("inside the Square class")
- Use static method when there is a method inside a class that is logically related to the class, but does not necessarily interact with any specific instance.
- Static methods are created using the @staticmethod decorator.
Class method
A class method is a method that is bound to the class and not the class’s object. Class methods know about their class. i.e. They can’t access specific instance dataIn class method the first argument in the function parameters is class, It’s an implicit first argument.
Class methods can access limited methods in the specified class, it can modify class specific details
class RECTANGLE:
name = "RECTANGLE"
def number\_of\_sides(self):
print(“I have 2 sides”)
@classmethod
def info\_class\_name(cls):
print(cls.name)
- Class methods are created using the @classmethod decorator.
- The class method has access to the class’s state as it takes a class parameter that points to the class and not the object instance.
Abstract method
An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units, we use an abstract class. An abstract class can be considered as a blueprint for other classes.
In Python Abstract class doesn’t have straight foreward implementation, We need to import abc(abstract base class). ABC works by decorating the base class’s methods as abstract and then registering concrete classes as implementations of the conceptual base.
from abc import ABC, abstractmethod
class POLYGON(ABC):
@abstractmethod
def number\_of\_sides(self):
pass
class RECTANGLE(POLYGON):
def number\_of\_sides(self):
print(“I have 2 sides”)
- Abstract methods are created using the @abstractmethod decorator.
- They override the properties of base class.
I hope this article gives the gist in understanding different types methods in Python OOPS inventory, Do share your thoughts in comments.