Examples

Example 5: @staticmethod called in the same class

Example 3: Class-level Data Manipulation (counter)

More Explanation


Things to note:

Prompt

<aside> 👤 Explain @classmethod to me for python

</aside>

Explanation

In Python, the @classmethod decorator is used to define a class method. A class method is a method that's bound to the class and not the instance of the class. It can be accessed and called directly on the class itself, without the need for creating an instance of the class.

Here's an example to illustrate the usage of @classmethod:

class MyClass:
    class_attribute = "Hello"

    def __init__(self, instance_attribute):
        self.instance_attribute = instance_attribute

    @classmethod
    def class_method(cls, parameter):
        print(cls.class_attribute)
        print(parameter)

# Calling the class method without creating an instance
MyClass.class_method("World")

In the above example, class_method is defined as a class method using the @classmethod decorator. When calling class_method, there's no need to create an instance of MyClass. The cls parameter refers to the class itself, allowing you to access class-level attributes or perform class-specific operations.

Note that the @classmethod decorator is not required for defining regular instance methods. Regular instance methods are automatically bound to the instance when called, and they receive the instance as the first parameter, conventionally named self.

To summarize, @classmethod is used to define methods that are bound to the class rather than the instance, allowing you to perform operations or access class-level attributes directly on the class itself, without the need for object instantiation.

<aside> 👤 So, that means you can either use class_method() like you normally would, or you can use it as a standlone function? Kind of like using the static method?

</aside>

Yes, that's correct! @classmethod allows you to use the class method in two different ways:

  1. Calling it on the class directly: You can call the class method directly on the class itself, without creating an instance. The class is passed as the first parameter (cls by convention), and you can perform operations or access class-level attributes using this parameter.
  2. Calling it on an instance: Although it's not commonly used, you can also call the class method on an instance. In this case, the instance is automatically passed as the first parameter (cls), just like with regular instance methods. However, it's more common to call class methods directly on the class.

Class methods are different from instance methods because they are not bound to any specific instance. They are associated with the class itself, and they operate on class-level data. This is why class methods are often used to define utility methods or alternative constructors that don't require an instance.

On the other hand, static methods, defined using the @staticmethod decorator, are not bound to either the class or the instance. They behave like regular functions but are defined inside a class for organizational purposes. Static methods don't have access to the class or instance, and they are typically used for utility functions that don't depend on class-level or instance-level data.