Introduction
Welcome to my 69th blog post on the Python journey. On day 69, I learned about the Class Method that is used to directly make an impact on the class variables and not the instance of the class. Let's dive into more details and understand Class Methods in Python.
So let's get started......
Class Methods
These methods are bound to the class as a whole and not the instance of it.
Class methods are defined using the "@classmethod" decorator, followed by a function definition.
The first argument in the function is taken as "cls" always, this represents the class itself.
Why Use Python Class Methods?
They are useful in several situations, such as you might want to create a factory method that creates instances of your class in a specific way. You could define a class method that creates the instance and returns it to the caller.
Another common use case is to provide alternative constructors for your class. This can be useful if you want to create instances of your class in multiple ways, but still have a consistent interface for doing so.
How to Use Python Class Methods
Simply use the "@classmethod" decorator before the method definition to define the class method.
The first argument of the method should always be "cls," which represents the class itself and then we can pass the arguments to it. Here is an example of how to define a class method:
Example
class ExampleClass:
@classmethod
def factory_method(cls, argument1, argument2):
return cls(argument1, argument2)
In the above example, the "factory_method" is a class method. It takes two arguments, i.e "argument1" and "argument2." It creates a new instance of the class "ExampleClass" using the "cls" keyword, and returns the new instance to the caller.
Important Note -
Class methods cannot modify the class in any way. If you need to modify the class, you should use a class level variable instead.
Resources Used
You can watch the video of Day#69 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 69, I learned about the special method i.e class method that can be used to define functions that operate on the class as a whole, rather than one specific instance. I also learned about the syntax of the class method and its use case.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #69
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on a daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.