Sobes.tech
Junior — Middle

What parameter is automatically passed to a method marked as @classmethod when it is called?

sobes.tech AI

Answer from AI

A method decorated with @classmethod automatically receives the first parameter — the class, not an instance.

This parameter is usually called cls. It allows the method to work with the class itself, rather than a specific object.

Example:

class MyClass:
    @classmethod
    def create_instance(cls, value):
        return cls(value)

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

obj = MyClass.create_instance(10)
print(obj.value)  # Will output 10

Here, cls refers to the MyClass class, and the method can create new instances of the class.

What parameter is automatically passed to a method… - sobes.tech