python - View 3+ more - Difference between staticmethod and classmethod

python class(object) vs class / python / oop / methods / python-decorators

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?

Anand Tripathi



Answer #1
def f(self, x, y)

When the method is decorated

@classmethod
def f(cls, x, y)
@staticmethod
def f(x, y)
class Point
    def __init__(self, x, y):
        self.x = x
        self.y = y

    @classmethod
    def frompolar(cls, radius, angle):
        """The `cls` argument is the `Point` class itself"""
        return cls(radius * cos(angle), radius * sin(angle))

    @staticmethod
    def angle(x, y):
        """this could be outside the class, but we put it here 
just because we think it is logically related to the class."""
        return atan(y, x)


p1 = Point(3, 2)
p2 = Point.frompolar(3, pi/4)

angle = Point.angle(3, 2)