Chat Zalo Chat Messenger Phone Number Đăng nhập
Class method vs Static method in Python - GeeksforGeeks

Class method vs Static method in Python – GeeksforGeeks

In this article, we will cover the basic difference between class method vs. static method in Python and when to use class method and static method in python.

What is the class method in Python?

The

@classmethod decorator is an integrated function decorator that is an expression that is evaluated after the function is defined. The result of this evaluation casts a shadow over the definition of the role. A class method receives the class as an implicit first argument, just as an instance method receives the instance

Python syntax Class method: class

C(object): @classmethod def fun(cls, arg1, arg2, …): …. Fun: The function that needs to be converted to a class method returns: a class method for the function. A class method is a method that is bound to the class and not to the object of the class. They have access to the state of the class because it takes a class parameter that points to the class

  • and not
  • to the instance of the object. You can modify a class state that would apply to all instances

  • of the class.
  • For example, you can modify a class variable that will be applicable to all instances.

What is the static method in Python?

A static method does not receive an implicit first argument. A static method is also a method bound to the class and not to the object of the class. This method cannot access or modify the state of the class. It is present in a class because it makes sense for the method to be present in class.

Python Static Method

: class C(object): @staticmethod def fun(arg1, arg2, …): … returns: a static method for the fun function. Class method

vs

static method

The difference between the Class method and

the static method is:

  • A class method takes cls as the first parameter, while a static method does not need specific parameters.
  • A class method can access or modify

  • the state of the class, while a static method cannot access or modify it
  • .

  • In general, static methods know nothing about class state. They are utility-type methods that take some parameters and work on those parameters. On the other hand, class methods must have class as a parameter.
  • We use @classmethod decorator in python to create a

  • class method and we use @staticmethod decorator to create a static method in python.

When to use the class or static method?

  • We generally use the class method to create factory methods. Factory methods return class objects (similar to a constructor) for different use cases.
  • We generally use static methods to create utility functions.

How to define a class method and a static method?

To define a class method in python, we use @classmethod decorator

, and to define a static method we use @staticmethod decorator. Let’s look at an example to understand the difference between the two. Let’s say we want to create a class person. Now, Python doesn’t support overloading methods like C++ or Java, so we use class methods to create factory methods. In the following example we use a class method to create a person object from the year of birth.

As explained above, we use static methods to create utility functions. In the following example we use a static method to check if a person is an adult or not.

A simple example

:

class

method:

static method:-

Below is the full implementation output

:21 25

Trueauxiliary space: O(1)

Contact US