Skip to content
Tutorials Center
My learning

Topics

Lesson 83 of 149 ยท Object-Oriented Programming

Metaclasses, Descriptors and Object Internals

Look inside Python objects. Learn how __dict__ and attribute lookup work, save memory with __slots__, write descriptors, and meet metaclasses.

So far you've used classes from the outside: define attributes, write methods, add properties. This lesson opens the hood. Where does Python actually store an object's attributes? How does self get passed to a method automatically? How does @property intercept attribute access? And since classes are objects too, what creates them?

The answers are a handful of mechanisms: the instance __dict__, __slots__, descriptors, __init_subclass__ and metaclasses. You won't need them in everyday code, but they power the tools you use daily (properties, methods, data classes, Django models, SQLAlchemy columns, Pydantic fields), and understanding them makes the rest of Python feel far less magical. The last section explains when you should not use them, which is most of the time.

Where attributes live: __dict__

A normal object stores its attributes in a dictionary called __dict__. The class has its own __dict__ too, holding methods and class attributes. The built-in vars() returns the same dictionary:

Python Instance and class dictionaries

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.

This shows the basic lookup order for obj.name: Python checks the instance's __dict__, then the class, then the parent classes in MRO order. Assigning obj.name = value always writes to the instance, which is why fern.kingdom = ... shadowed the class attribute instead of changing it. (There's one important exception to this order, data descriptors, which you'll meet below.)

Fallbacks with __getattr__

If normal lookup fails, Python calls __getattr__(self, name) as a last resort, if the class defines it. That makes it handy for wrappers and objects with dynamic fields:

Python __getattr__ as a fallback

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.

Raise AttributeError (not KeyError) for missing names, so hasattr() and getattr() with a default keep working. Its sibling __getattribute__ runs for every attribute access; overriding it is rarely a good idea and easily causes infinite recursion.

Saving memory with __slots__

A dictionary per object is flexible but costs memory. When you create millions of small objects, you can declare a fixed set of attributes with __slots__. Python then stores them in compact fixed slots, with no __dict__:

Python __slots__

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.

Slots also make attribute access slightly faster and catch typos like p.xx = 1. The trade-offs: you can't add attributes at runtime, class-level default values for slot names aren't allowed, and every class in an inheritance chain must declare __slots__ for the savings to hold. Use them for small, numerous objects. With data classes, @dataclass(slots=True) writes them for you, as you saw in Data Classes.

Descriptors: objects that control attribute access

A descriptor is an object, stored as a class attribute, that defines __get__, __set__ or __delete__. When you access that attribute on an instance, Python calls those methods instead of reading or writing the value directly.

That's exactly how @property works. Writing your own descriptor lets you reuse the same attribute logic across many classes and fields, where a property would need copy-pasted getters and setters:

Python A reusable validated field

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.

Here's what happens step by step:

  1. When class Product is created, Python calls __set_name__ on each descriptor, telling it its own name ("price", "weight_kg").
  2. self.price = 39.0 finds Positive on the class with a __set__ method, so it calls Positive.__set__(descriptor, lamp, 39.0), which validates and stores the value in lamp._price.
  3. lamp.price calls Positive.__get__(descriptor, lamp, Product), which reads lamp._price.

One descriptor instance is shared by every Product, which is why the value lives on the instance (obj), never on the descriptor. This pattern is the heart of ORMs and validation libraries: name = models.CharField(max_length=100) in Django is a class-level field object of this kind.

Data and non-data descriptors

The lookup order has two kinds of descriptor:

  • A data descriptor defines __set__ (or __delete__). It takes priority over the instance's __dict__. Properties and Positive are data descriptors, so assignment can't bypass them.
  • A non-data descriptor defines only __get__. The instance __dict__ wins over it. Functions are non-data descriptors, and that's how methods work:
Python Methods are descriptors

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.

So self isn't magic: accessing g.hello calls the function's __get__, which returns a bound method that remembers g and passes it as the first argument. classmethod and staticmethod are descriptors too; they just bind differently.

Hooking into subclass creation: __init_subclass__

Sometimes a base class wants to react whenever someone subclasses it: register the new class, check that it defines required attributes, or set defaults. Define __init_subclass__ on the base class, and Python calls it for every new subclass:

Python A plugin registry with __init_subclass__

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.

Keyword arguments in the class statement (extension="csv") are passed to __init_subclass__. Adding a new export format is now just a matter of writing a class; no list to update anywhere else. Always call super().__init_subclass__(**kwargs) so that cooperating base classes still get their turn.

Metaclasses: the classes of classes

Every object has a type, and classes are objects, so classes have a type too. For almost all classes it's type. type is the metaclass: the class whose instances are classes. Calling it with three arguments creates a class, which is exactly what a class statement does behind the scenes:

Python type creates classes

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.

A custom metaclass is a subclass of type that customizes class creation. Its __new__ receives the class's name, bases and namespace dictionary before the class exists, so it can inspect or change them. This one enforces a naming rule and records every class it builds:

Python A simple metaclass

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.

Metaclasses are inherited, so every subclass of Model goes through CheckedMeta. That's how frameworks apply rules to every model class you write. The standard library's abc.ABCMeta (behind abstract base classes) and enum.EnumType (behind enums) are metaclasses.

Metaclasses have real costs: a class can have only one metaclass, so combining two libraries that each use one can fail with a "metaclass conflict"; and the behavior is hidden far from the code it affects.

When not to use these tools

Metaclasses are deeper magic than 99% of users should ever worry about.

That often-quoted line from Python core developer Tim Peters still holds. Reach for the simplest tool that solves the problem:

  1. Plain attributes and methods. Most classes need nothing more.
  2. @property for one computed or validated attribute.
  3. A descriptor when the same attribute logic repeats across many fields or classes.
  4. A class decorator (like @dataclass) to modify a class once after it's created.
  5. __init_subclass__ to react to every subclass: registration and checks.
  6. A metaclass only when you need to control class creation itself, such as changing the namespace before the class exists, usually inside a framework.

Every step down this list makes code more powerful and harder for the next reader to follow. If a teammate would need this lesson to understand your class, consider whether a simpler design would do.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. p.color = "red" runs on an instance whose class has a class attribute color = "blue" (not a descriptor). What happens?

    Assignment writes to the instance dictionary. The class attribute stays "blue" for everyone else.
  2. 2. What is the main trade-off of declaring __slots__ = ("x", "y")?

    Slots replace the per-instance __dict__ with fixed storage, which saves memory and blocks new attribute names.
  3. 3. Which method lets a descriptor learn the attribute name it was assigned to?

    When the owning class is created, Python calls __set_name__(owner, name) on each descriptor in the class body.
  4. 4. How does obj.method get self filled in automatically?

    Accessing a function through an instance calls function.__get__(obj, cls), which returns a bound method.
  5. 5. You want every subclass of Plugin to be added to a registry automatically. What's the simplest tool?

    __init_subclass__ runs for each new subclass and covers registration and checks without the costs of a metaclass.

Summary

  • Instance attributes live in obj.__dict__ (see it with vars()); lookup checks the instance, then the class and its parents, and assignment writes to the instance.
  • __getattr__ is a fallback for failed lookups; raise AttributeError for unknown names.
  • __slots__ replaces the per-instance dictionary with fixed slots: less memory, no new attributes.
  • Descriptors (__get__, __set__, __set_name__) control access to class-level attributes; properties, methods, classmethod and ORM fields are all descriptors. Data descriptors beat the instance __dict__; non-data descriptors don't.
  • __init_subclass__ runs for every new subclass; it's ideal for registries and checks.
  • type is the default metaclass; custom metaclasses subclass type to control class creation, and should be a last resort.

Next: Type Hints, where you'll annotate your code so editors and type checkers can catch bugs before you run it.