The @classmethod decorator is indeed a built-in method decorator that is evaluated once your function has been declared. The outcome of that assessment casts doubts over your function definition. In the same way, an instance method receives the instance as an implicit first parameter; a class method gets the class as an implicit first argument. An implicit initial parameter is not passed to a static method. Static methods are connected to the class. The class state cannot be accessed or modified by a static method. It’s in a class, as it clearly explains why the method should be in a class. Now let’s look at some examples.
A static method is a broad utility method that accomplishes a single task. Python’s static measures are related to those in Java and C++. A static method is directly connected to the class and not with the object of the class. As a result, we can refer to it by its class name. A static method has no access to the class as well as, for instance, variables since it does not get a starting argument such as cls and self. It is not possible to change the object’s or class’s state as an outcome. ClassName.method_name() and an object of the class can be used to call the class method.
print('Inside static method', a)
std = Student()
Here you can see the generated result.
Some programs may establish a static method by invoking staticmethod() as a function but instead as a decorator. If you need to support previous versions of Python, you should only use the staticmethod() function to define static methods. If you don’t have to use the @staticmethod decorator, be using the @staticmethod decorator instead. The staticmethod() is useful in situations where you look for a connection to a function from a class body but not in favor of the automated transition to the instance method. In the given an example, you can see how to call a static method from some other static method in the same class. We’ll distinguish a static method from a class method in this section.
print('static method 1')
def static_method_Two() :
def class_method_One(cls) :
Here is how you can call a static method.
We’ll define a class method and then a static method in this example. The purpose of a @staticmethod decorator is to define a static method, and the @classmethod decorator is to define a class method. See the example. Let’s imagine we want to make a Person class. Because Python does not enable method overloadings like C++ or Java, we must define factory methods using class methods. In the example below, we utilize a class function to build a person object from a birth year. To establish whether or not a person is an adult, we use a static method in the example below.
def __init__(self, your_name, your_age):
self.your_name = your_name
self.your_age = your_age
def fromBirthYear(cls, your_name, your_year):
return cls(your_name, date.today().year - your_year)
return your_age> 18
First_p = Person('Alex', 23)
Second_p = Person.fromBirthYear('Alex', 1989)
See the result below.
Difference Between Class method and Static Method
A static process does not involve cls as the initial parameter, but a class method does. A class method can also access the class state and change it, but a static method does not. Static methods usually have no idea of the state of the class. They are utility methods that take parameters and perform operations on them. Class methods should consider class as a parameter. In Python, the @staticmethod decorator is frequently used to develop a static method, whereas the @classmethod decorator is widely used to construct a class method.
This tutorial covered the basics of class methods, static methods, and ordinary instance methods. A class instance is not required for class methods. They can’t get to the instance, but they can get to the class (cls). Cls and self are not accessible to static methods. Also, they operate in the same way as regular functions, but they’re in the namespace of the class. Static and class methods exchange information and (to some extent) enforce developer intent on class design. This even has the ability to be advantageous in terms of upkeep. You’ll be able to create object-oriented Python that conveys its goal more clearly and is easier to maintain in the long term if you gain an intuitive knowledge of their distinctions.