Class Methods and Static Methods in Python @mecademie
Class Methods and Static Methods in Python  @mecademie
Uploaded December 2024 | Updated November 2026, 3 hours ago
#static #class #pyhton #oop #method
In this lecture, we're going to dive into two important concepts in Python classes: Class Methods and Static Methods.
We'll explore how they differ from regular instance methods and what makes them useful in specific situations. By the end of the lecture, we'll understand how and when to use each method type in our programs.

So, what are class methods?
Well, regular methods inside a class in Python work with instances, right?
When we define a method and use self, we're working with an instance of that class.
But sometimes, we don't need to work with an instance; instead, we need to interact with the class itself. That's where class methods come in.
These methods can access or modify the class state, but they don't work with individual object data.
Class methods are marked with the @classmethod decorator.
Instead of receiving an instance as the first parameter (like self), class methods take 'cls', which refers to the class itself.
Let's break it down with some code.
First, let's create a new Python file in the class folder for this lecture. We'll call it 'methods_demo.py'.
Inside this file, let's start with a simple class.
Here, we've created a class called Employee with a class variable company_name.
We then defined a class method called change_company_name.
Notice how it uses cls to change the class-level variable.
We aren't working with instances here, just the class itself.
Now, let's create two instances of the Employee class.
After creating two employees, we can verify that both share the same company_name by printing it through each one.
Now, we can call the change_company_name method to update the class variable.
It will change the company name for the whole class, not just for an instance.
We can then verify the new name via each of the instances by printing it again.
If we run the script,
We will observe the company name via each employee before and after it changes.
What happened here?
We changed the class variable itself, and that change was reflected in all instances, without having to directly interact with them.

Now, let's talk about static methods.
These methods don't deal with class or instance data at all.
They behave just like regular functions, but we include them inside a class to group related functionality together.
Static methods are defined using the @staticmethod decorator.
Unlike class methods, they don't take self or cls as their first argument.
Instead, they're just simple functions that live inside the class for organization.
Let's add a static method to our Employee class.
Here, we've added a static method called is_valid_age.
It takes a parameter age, but it doesn't interact with any instance or class variables.
Let's check if 25 is a valid age.
If we run the code again,
We'll see that 25 is a valid age as it is greater than 18.
This shows that the static method works independently of any class data.
It's just a utility function that we've bundled inside the class because it makes sense to put it there.

By now, you might be wondering when to use each type of method.
Here's a quick summary.
Instance methods are the most common and are used when we need to access or change data specific to an instance using self.
Class methods come in handy when we need to work with class-level data shared across all instances, using cls.
Lastly, static methods don't rely on class or instance data but are included in the class because they're logically related to it.
Let's take a moment to compare them all inside the Employee class.
First, we define some instance attributes via adding a constructor method.
The self keyword is used here to refer to the current instance of the class.
Next, we define an instance method, called get_details, which will allow us to access these attributes and return them in a readable format.
We previously created our employees using the default constructor.
But, now that we've added the new constructor, we can update our instances by providing the employee's name and age.
Once we have created our instances, we can use the get_details method to retrieve and display their information.
If we run the script again,
We'll notice that all the three types of methods behave exactly as expected.
The instance method gives us details about our employees.
The class method changes the company name.
And, the static method checks if the given age is valid.

To summarize, in this lecture, we've covered three types of methods in Python classes.
Class methods work with the class itself and are marked by @classmethod.
We use these for class-level changes, like modifying class variables.
Static methods are standalone functions within a class that don't interact with instance or class variables.
They're defined using @staticmethod and are used for utility functions that belong logically to the class.
We also touched on instance methods, which interact with individual instances using self.
By now, we should have a good understanding of how each...
Class Methods and Static Methods in PythonFiltering The Road Profile By The Rigid Tread Band Tire Model - by Shahram ShokouhfarAssembling Process of a System using Newtons Iterative MethodDeal with revolute joints between convergent links when finding the degrees of freedom in a systemAbstraction in PythonRolling Centrodes of a Crank-Crank Four-Bar Linkage (Considering C13)Class Attributes vs Instance Attributes in PythonShaft with Uniuversal Joints (Parallel Yokes)Slider-Crank MechanismFile I/O OperationsOperators in PythonContact Modeling in MASS Software
mecademie |

Class Methods and Static Methods in Python

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER