Code Reference

Classes

Python · Reference cheat sheet

Classes

Python · Reference cheat sheet


📋 Overview

Classes define object types: attributes + methods. Prefer clear __init__, type hints, and small cohesive classes. For data-heavy records, consider dataclasses first.

🔧 Core concepts

PieceRole
class Name:Creates a type object
__init__Instance initializer
selfInstance reference (conventional name)
Instance attrself.x = ...
Class attrShared on the class body
@staticmethodNo self / cls
@classmethodReceives cls
@propertyManaged attribute

Methods are functions on the class; binding supplies self on instance access.

💡 Examples

Basic class:

class User:
    species = "human"  # class attribute

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

    def greet(self) -> str:
        return f"Hi, I'm {self.name}"

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> "User":
        return cls(str(data["name"]), int(data["age"]))  # type: ignore[arg-type]

    @staticmethod
    def is_adult(age: int) -> bool:
        return age >= 18

u = User.from_dict({"name": "Ada", "age": 36})
print(u.greet(), User.is_adult(u.age))

Property:

class Temperature:
    def __init__(self, celsius: float) -> None:
        self._celsius = celsius

    @property
    def celsius(self) -> float:
        return self._celsius

    @celsius.setter
    def celsius(self, value: float) -> None:
        if value < -273.15:
            raise ValueError("below absolute zero")
        self._celsius = value

Slots (memory / attr lock):

class Point:
    __slots__ = ("x", "y")

    def __init__(self, x: float, y: float) -> None:
        self.x = x
        self.y = y

⚠️ Pitfalls

  • Mutable class attributes are shared across instances — use instance attrs in __init__.
  • Forgetting self in method signatures causes confusing TypeErrors.
  • __init__ should not return a value (except None).
  • String annotations / from __future__ import annotations affect runtime typing.get_type_hints.
  • Prefer composition over deep inheritance trees.

On this page