Practical OOP in Python: Methods
Understanding the practical applications and most suited use-case of methods

Search for a command to run...
Understanding the practical applications and most suited use-case of methods

No comments yet. Be the first to comment.
This is my attempt to guide you on practical and useful tips, concept that will help you to develop a good software...because its just an idea that all you need 💡.
Uncover why SQL may not be the best choice for ETL pipelines in data applications and learn about common hurdles.
Bringing in a fresh perspective on how you can turn your traditional streaming pipeline to perform like batch processing with endless possibilities

Uncover why SQL may not be the best choice for ETL pipelines in data applications and learn about common hurdles.

Turning Data Pipeline Failures into Success Stories: A Memory Optimization Case Study with Polars Library & Data Engineering best practices.

Practical techniques backed by benchmarks for working with Databases effectively in Python

Guide to Efficiently Streamlining Your Databricks Environment Setup

The class is the backbone of OOP in Python and methods are body parts of the class. Understanding the practical application of the methods is the key to take the most advantage of the class and eventually OOP in Python.
Let's touch on some theory in brief as this blog focuses on practicality
class: A class is a user-defined blueprint or prototype from which objects are created. It wraps all the similar methods (ideally by conventions).
methods: A glorified function that is a class member and will always remain bound to a class.
If you wish to understand the theory, then I would suggest you go through
Types of methods available in Python:
The beauty of OOPs in any programming language (that has its support obviously 😎) is the flexibility. There will be always more than one way to do any task at hand, but not all are best practice or practical or pythonic.
Note: I will only touch the syntax/theory part briefly with an assumption that the reader knows the theory part but to see their real-world practical use-case.
Let's begin by writing a sample class that will be used across the complete blog
@dataclass
class SampleClass:
"""This is a sample class to explain practical use-case of all methods
"""
xyz: int
abc: int = 4
def ins_method(self, no: int):
"""Sample instance method
Args:
no (int): any int number
"""
print(self.xyz * no)
@classmethod
def cls_method(cls, no: int):
"""Sample class method
Args:
no (int): any int number
"""
print(cls.abc * no)
@staticmethod
def stc_method(no: int):
"""Sample static method
Args:
no (int): any int number
"""
print(SampleClass.xyz * no)
Now we'll see their practical use case one by one.
SampleClass example ins_method() is the instance method. @dataclass
class SampleClass:
xyz: int
abc: int = 4
def ins_method(self, no: int):
print(self.xyz * no)
s = SampleClass(xyz=10)
s.ins_method(2)
# Output
20
Factory function: Before moving to the next part, it is important to understand factory function) as this is one use-case where
classmethodandstaticmethodhave wide practical application. Again here I will be focussing on practical.
@dataclass
class SampleClass:
xyz: int
abc: int = 4
@classmethod
def cls_method(cls, no: int):
print(cls.abc * no)
SampleClass(4).cls_method(2)
# Output
8
SampleClass but directly used '.' notation with 'SampleClass' itself as classmethod can be bound to class directly. If you compare it with the instance method, there we created an object s then the method ins_method() was being accessed as it is bound to s but not to Sampleclass@dataclass
class SampleClass:
xyz: int #This is instance variable
abc: int = 4 # This is class variable
@classmethod
def cls_method(cls, no: int):
print(cls.abc * no)
# Traditional way
class SampleClass:
abc: int = 4 #This is class variable
def __init__(self, xyz):
self.xyz = xyz # This is instance variable

cls_method() only have access to abc which is class variable
ins_method() have access to everything. @dataclass
class SampleClass:
xyz: int
abc: int = 4
@classmethod
def cls_method(cls, no: int):
print(cls.abc * no)
SampleClass(4).cls_method(2) # Here cls_methos is using class variable - 'abc' as well as user input - 'no'
# Output
8
classmethod. Some dev argues that it is totally useless, see here.classmethod except access to neither class variable nor to instance variable as it does not use either self or cls. classmethod it can be used as a factory function, but should not. Becauseclassmethod for it. classmethod will work perfectly even if it is not using a class variable. staticmethod as it does not need any class/instance variable. This is a special type of methods, in fact formally it falls under descriptor. Follow this excellent blog if you want to understand the theory part
@property should be used when you want to return an attribute produced by some function/method.
instance method name. By seeing this name convention, the user will understand that this particular should not be touched. @dataclass
class SampleClass:
xyz: int
abc: int = 4
def ins_method(self, no: int):
print(self._pvt_method(no) * no)
def _pvt_method(self, no: int):
return (self.abc * no)
But as mentioned earlier that Python doesn't have a true private method, so the user can still use it. Someone who doesn't the name convention might accidentally use it.
sc = SampleClass(4)
sc._pvt_method(4)
# Output
16
instance method name. @dataclass
class SampleClass:
xyz: int
abc: int = 4
def ins_method(self, no: int):
return (self.__strict_pvt_method(no) * no)
def _pvt_method(self, no: int):
return (self.abc * no)
def __strict_pvt_method(self, no: int):
return (self.abc * no)
# Output
sc.ins_method(4)
64
sc._pvt_method(4)
16
sc.__strict_pvt_method(4)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-31-102204654d54> in <module>
----> 1 sc.__strict_pvt_method(4)
AttributeError: 'SampleClass' object has no attribute '__strict_pvt_method'
sc._SampleClass__strict_pvt_method(4)
# Output
16
Note: I will suggest to further read this thread