- Demystifying Python OOP (Part 3) — Python Static, Class and Abstract methods
- Instance / Object methods:
- Class Methods:
- Static Methods:
- Abstract Methods:
- Static Class in Python
- Use the @staticmethod Decorator to Create a Static Class in Python
- Use the @classmethod Decorator to Create a Static Class in Python
- Use a Module File to Create a Static Class in Python
- Related Article — Python Class
Demystifying Python OOP (Part 3) — Python Static, Class and Abstract methods
Many times while code reviews, I have seen people defining their classes and methods but incorrectly using decorators that Python provides for methods. So today I am going to talk more about these decorators and how and why to use them. We will see couple of real world scenarios where we can use them.
In this blog post, we will first take an example and then will break it down to dig more into these concepts. So here’s our first example:
class ExampleClass(object): def some_method(self): pass @classmethod def class_method(cls): pass @staticmethod def static_method(): pass
Instance / Object methods:
In our example above, some_method() is called the object method or instance method. The method takes one paramater self, which points to an instance of class ExampleClass when the method is called.
As we already know, using self parameter, instance methods can access member functions and other member variables of the class. This is the method that you will use most of the times while programming in Python.
Let’s take a simplistic example:
Let us say we have a simple Animal class. Animal class has a member variable as count that stores the count of the total Animals.
class Animal(object): count = 1 def get_count(self): return self.count # access the get_count() by instance of the class animal = Animal() animal.get_count() # 1
So we can access the get_count method by referencing class instance animal . What would happen if we try to do this:
class Animal(object): count = 1 def get_count(self): return self.count print(Animal.get_count()) # TypeError: get_count() missing 1 required positional argument: 'self'
When executing above, we get TypeError: get_count() missing 1 required positional argument: ‘self’ . This means the methods of the class can only be called by referencing to instances of that class. If we want to use Animal.get_count() as it is, we can make use of class methods.
Class Methods:
Class methods in Python can be defined by assigning @classmethod to any method. The thing to note here is that the class methods take cls parameter that points to a class and not the instance of the class.
Well what does that mean? Let us break this down. class_method() takes cls as argument that means any class that we specify here is able to access to its own members and not the instance state. I know it’s a bit hard to understand. Hence, let us take a simple example to see what exactly are class methods.
We have our Animal class defined as:
class Animal(object): count = 1 def get_count(self): return self.count # access the get_count() by instance of the class animal = Animal() animal.get_count() # 1
Now, if we want to increase the count by accessing the class itself and not by its instance, we can make use of class method like:
class Animal(object): count = 1 def get_count(self): return self.count @classmethod def inc_count(cls): cls.count += 1 # increment the count one return cls() animal = Animal.inc_count() # inc count by accessing class directly animal.get_count() # 2 animal.inc_count() # inc count by referencing the class instance (not recommended) animal.get_count() # 3
So long story short, class methods give us the power to access a method using reference to class itself rather than instance of that class.
Another thing to note here that self and cls are just the naming conventions that are followed by most of the Python programmers. You can give any name for those parameters just the mandatory thing is that they should be placed first in the arguments list.
Static Methods:
Now, let’s see what are static methods. Like any function that we call, static methods can be called in a similar way. Hence, static methods are like regular functions just with the fact that they belong to a class’s namespace.
Static methods can be defined by decorating methods with @staticmethod decorator.
As static methods do not take self and cls as parameters, they do not have access to class members and variables.
class Example(object): @staticmethod def just_another_method(): print('This is static method') example = Example() example.just_another_method() # This is static method
Abstract Methods:
Abstract methods in Python are pretty much different than class methods and static methods. However, while writing Object Orientated programs, abstract methods are used often.
Abstract methods in Python are the methods that are defined in the base class, but do not have any implementation. The derived class must override these abstract methods in their definition. Failing to do so will cause NotImplementedError .
For those who are familiar with Java programming language, abstract method in Python are equivalent to interface methods in Java.
class BaseClass(object): def do_something(self): raise NotImplementedError
Any class that will inherit BaseClass should override and implement the do_something() method, otherwise an exception would be thrown.
All content is licensed under the CC BY-SA 4.0 License unless otherwise specified
Static Class in Python
- Use the @staticmethod Decorator to Create a Static Class in Python
- Use the @classmethod Decorator to Create a Static Class in Python
- Use a Module File to Create a Static Class in Python
A static class is a handy feature in programming. A static class cannot be inherited and cannot be instantiated. There is no direct way to make a class static. In Python, we can implement a static class by making its methods and variables static.
In this article, we will implement such methods.
Use the @staticmethod Decorator to Create a Static Class in Python
To implement a static class, we can make its methods and variables static. For this, we can use the @staticmethod decorator to create methods in a class static. A decorator is a special function specified before a function and takes the whole function as a parameter.
class abc(object): @staticmethod def static_method(): print("Hi,this tutorial is about static class in python") abc.static_method()
Hi,this tutorial is about static class in python
Note that this method only works on versions of Python higher than 2.6.
Use the @classmethod Decorator to Create a Static Class in Python
The @classmethod can make a method static to the class and not its object. It has several advantages over the @staticmethod decorator. It will also work with subclasses and can modify the class state.
However, it requires a compulsory argument to be included in the method called cls .
class abc(object): @classmethod def static_method(cls): print("Hi,this tutorial is about static class in python") abc.static_method()
Hi,this tutorial is about static class in python
Use a Module File to Create a Static Class in Python
In Python, probably the best way to implement a static class is by creating a module. We can import a module, and its functions can be accessed using the module name. It does not require any objects to be instantiated, and it cannot be inherited.
See the following example.
def static_method(): print("Hi this is the content of the module file")
The above code can be a simple function in a Python script that can be imported as a module.
For example, if the name of the above Python script is abc.py , we can do something as shown below.
import abc abc.static_method()
Hi,this tutorial is about static class in python
Related Article — Python Class
Copyright © 2023. All right reserved