Last modified: Dec 01, 2026

Python Class Methods Examples

Class methods are a core part of object-oriented Python. They let you work with the class itself, not just one object. This guide walks through clear examples you can reuse.

We will cover instance methods, class methods, and static methods. Each one has a purpose. Once you see them side by side, the differences become easy to remember.

What Is a Method in Python?

A method is a function defined inside a class. It always receives the object or class as its first argument. That first argument is how the method knows what it is working on.

For instance methods, that first argument is usually named self. If you are new to this idea, this explanation of what self means in Python is a good starting point.

Python is not the only language with this concept. Java uses this instead. If you come from Java, this comparison of Python self vs Java this will help you map the idea across languages.

Instance Methods: The Default

Most methods you write are instance methods. They take self and operate on a single object. They can read and change the object's data.


class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):  # instance method
        return f"{self.name} says woof!"

dog = Dog("Rex")
print(dog.bark())

Rex says woof!

Notice that bark uses self.name. It depends on a specific dog. That is the key trait of an instance method.

Class Methods: Working With the Class

A class method takes cls as its first argument. It works with the class, not one object. You create one with the @classmethod decorator.

Class methods are great for alternative constructors. They can build an object in a different way. They can also read or change class-level data.


class Person:
    species = "human"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def from_string(cls, data):  # alternative constructor
        name, age = data.split(",")
        return cls(name, int(age))

    @classmethod
    def get_species(cls):  # reads class data
        return cls.species

p = Person.from_string("Ana,30")
print(p.name, p.age)
print(Person.get_species())

Ana 30
human

The from_string method returns a new Person. It uses cls so subclasses get the right type. That makes class methods flexible and safe.

Static Methods: No Self, No Cls

A static method takes no special first argument. It is just a function that lives inside the class. Use @staticmethod to define one.

Static methods are useful for helper logic. They do not touch object data or class data. They keep related code close to the class.


class Math:
    @staticmethod
    def add(a, b):  # no self or cls
        return a + b

print(Math.add(3, 4))

7

You can call Math.add directly on the class. You do not need an instance. That is the main benefit of static methods.

Methods vs Properties

Sometimes a method is not the right tool. If a value should look like an attribute, a property may be better. Properties use the @property decorator.

Properties let you add logic to attribute access. You can validate values or compute results on the fly. To see the difference clearly, read this guide on Python property vs attribute.

If you want to add setters and getters with validation, this walkthrough of the Python class property setter pattern shows how to do it step by step.

Using cls for Class-Level State

Class methods can track shared state. A common example is counting how many objects exist. The counter lives on the class, so every instance sees it.


class Counter:
    count = 0

    def __init__(self):
        Counter.count += 1

    @classmethod
    def total(cls):
        return cls.count

a = Counter()
b = Counter()
print(Counter.total())

2

Here total reads cls.count. If you subclass Counter, the method still points to the right class. That behavior is a big reason to prefer cls over hardcoding the class name.

Inheritance and Class Methods

Class methods respect inheritance. When a subclass calls a class method, cls refers to the subclass. This makes factory methods reliable across a class tree.


class Animal:
    def __init__(self, name):
        self.name = name

    @classmethod
    def create(cls, name):
        return cls(name)

class Cat(Animal):
    pass

c = Cat.create("Milo")
print(type(c).__name__)

Cat

The create method returns a Cat, not an Animal. The class method adapted to the subclass automatically. This is powerful and easy to use.

Common Mistakes to Avoid

Beginners often forget the decorator. Without @classmethod, cls becomes a normal argument. The method then behaves like an instance method and breaks.

Another mistake is using self in a class method. It works, but it is confusing. Stick to cls so readers know the method works on the class.

Finally, do not use class methods for things that need instance data. If the logic depends on self.name or similar, an instance method is the right choice.

When to Use Each Method Type

Use an instance method when the logic needs object data. Use a class method for alternative constructors or class-level state. Use a static method for pure helper functions.

These rules are simple and cover most cases. When in doubt, start with an instance method. Refactor to a class method only when the class itself is the subject.

Conclusion

Python class methods are a small feature with big impact. They let you build objects in flexible ways and work with shared class data. Combined with instance and static methods, they give you a full toolkit.

Practice the examples above. Change them, break them, and fix them. That hands-on loop is the fastest way to master class methods in Python.