Skip to content
Tutorials Center
My learning

Topics

Lesson 75 of 149 ยท Object-Oriented Programming

Instance, Class and Static Methods

Learn the three kinds of methods in Python classes, when to use @classmethod and @staticmethod, and how to build clean alternative constructors.

In Classes and Objects you wrote methods that take self and work on one object. Those are instance methods, and they are by far the most common kind. Python has two more: class methods, which receive the class itself instead of an object, and static methods, which receive neither.

Knowing which one to reach for keeps your classes tidy. In this lesson you'll see how each kind is called, what it can access, and the classic job of class methods: alternative constructors such as Date.from_string("2026-10-14").

Instance methods: working with one object

An instance method is a plain def inside a class. When you call it on an object, Python passes that object in as the first argument, which by convention is named self:

Python Instance methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows what really happens. clicks.describe() looks up describe on the class and calls it with clicks as self. Because it gets the object, an instance method can read and change that object's attributes. It can also reach the class through type(self) when it needs to.

Class methods: working with the class

Put @classmethod above a method and Python passes the class as the first argument instead of an instance. The convention is to name it cls:

Python A class method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can call a class method on the class (Ticket.how_many()) or on any instance. Either way, cls is the class. That makes class methods the right tool for anything that is about the class as a whole: shared counters, registries, configuration, and especially building new objects.

Alternative constructors

A class has only one __init__, but data often arrives in different shapes: a string from a file, a dictionary from JSON, a tuple from a database. Instead of cramming every format into __init__, add class methods named from_something that parse the input and then call cls(...):

Python Alternative constructors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The standard library uses this pattern everywhere. dict.fromkeys(), datetime.fromisoformat() and int.from_bytes() are all class methods that build objects from a different kind of input. You'll meet datetime.fromisoformat() in Dates and Times.

Why cls(...) and not the class name?

You could write return Color(...) inside from_hex, and it would work, until someone makes a subclass. Calling cls(...) builds an object of whatever class the method was called on, so subclasses get the right type for free:

Python cls follows subclasses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Tile never defines square, yet Tile.square(4) returns a Tile, because cls was Tile for that call. You'll learn more about subclasses in the next lesson, Inheritance.

Static methods: plain functions in a class

A method marked @staticmethod receives no automatic first argument at all. It's an ordinary function that happens to live inside the class, usually because it's closely related to it:

Python A static method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line fails on purpose, so you can see the ValueError the constructor raises. is_strong doesn't need an instance (you want to check text before building a Password) and it doesn't need cls. Making it static documents exactly that: "this helper uses only its arguments."

Note: Static methods are optional. A module-level function would work just as well, and many Python developers prefer that. Use a static method when grouping the helper with the class makes the code easier to find and read.

Calling one kind from another

Methods can call each other freely, but the way you reach them depends on what you have in hand:

  • Inside an instance method, call anything through self: self.other_method(), self.is_strong(text) or self.from_hex(code). Python finds static and class methods through the instance just as it finds instance methods.
  • Inside a class method, use cls: cls.helper() or cls(...). There's no self, because no object exists yet.
  • Inside a static method, there's neither self nor cls. If you need the class, write its name, or ask yourself whether the method should really be a class method.

Calling through self or cls instead of the hard-coded class name has the same advantage you saw with cls(...): if a subclass replaces the helper, your call automatically uses the subclass's version.

One more detail: class attributes such as Ticket.issued are shared by every instance, but assigning self.issued = 5 inside an instance method creates a new attribute on that one object and leaves the shared value untouched. When you want to change shared state, assign it on the class, as Ticket.issued += 1 does. (Be aware that cls.issued += 1 called through a subclass creates a separate counter on that subclass.) Mixing the two up is a classic source of confusing bugs.

Choosing the right kind

Ask what the method needs:

Needs Use First parameter
One object's data instance method self
The class (to create objects or read shared state) @classmethod cls
Neither, only its arguments @staticmethod none

A common mistake is writing a class method that only ever uses its arguments. If you never touch cls, it's really a static method (or a module function). The reverse mistake, a static method that hard-codes the class name to call its constructor, breaks subclasses; that one should be a class method.

Putting it together

Here is a small class that uses all three kinds. Notice how each method's decorator tells you at a glance what it can touch:

Python All three together

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try adding a "300K" reading and a branch for Kelvin. Which kind of method should the conversion helper be?

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a method decorated with @classmethod receive as its first argument?

    A class method always receives the class (named cls by convention), whether you call it as Ticket.how_many() or t.how_many().
  2. 2. Why should an alternative constructor return cls(...) instead of naming the class directly?

    cls is whatever class the method was called on, so Tile.square(4) builds a Tile, not a plain Shape.
  3. 3. Which method is the best fit for @staticmethod?

    A static method gets neither self nor cls, so it suits helpers that depend only on their arguments.
  4. 4. Given c = Counter(), what is c.describe() equivalent to?

    Calling a method on an instance looks it up on the class and passes the instance as self.

Summary

  • Instance methods take self and work with one object's data. They're the default.
  • Class methods (@classmethod) take cls and work with the class: shared state and creating objects.
  • Alternative constructors such as from_hex or from_dict are class methods that end with return cls(...), so they work for subclasses too.
  • Static methods (@staticmethod) get no automatic argument; they're plain helper functions grouped with the class.
  • Pick the kind by asking what the method needs: an object, the class, or nothing.

Next: Inheritance, where you'll build classes on top of other classes and learn how super() works.