In short
How does @property turn a method into an attribute? How does Django know about every model the moment you define the class? The answer is descriptors and metaclasses, the machinery behind Python's own magic.
Descriptors control what happens when an attribute is read or set, and they power @property and methods. Metaclasses control how classes themselves are created. Both are rare in daily code but explain how frameworks like Django work.

Think of it like this
A society gate and its builder
A descriptor is the security guard at the gate: every time someone enters or leaves (sets or gets an attribute), the guard checks them. A metaclass is the builder who constructs the gate itself. Most days you only meet the guard. Once in a while you need to change how gates are built for every building in the city.
Watch it happen
What happens on p.price = -10
1/4Your code runs p.price = -10 inside Product.__init__ or anywhere else.
The big ideas
What you need to know
Descriptors
A class attribute with __get__, __set__ or __delete__ controls access to that attribute on every instance. property, methods and classmethod are descriptors.
__set_name__
Python calls it when the class is created, telling the descriptor which attribute name it was assigned to.
Metaclasses
A class is an object made by its metaclass, which is type by default. A custom metaclass can change or register classes as they are created.
Simpler tools first
Reach for @property, dataclasses or __init_subclass__ before writing a metaclass. Most problems don't need one.
See it in code
Walk through the code
1class Positive:2 def __set_name__(self, owner, name):3 self.private_name = "_" + name4 5 def __get__(self, obj, objtype=None):6 if obj is None:7 return self8 return getattr(obj, self.private_name)9 10 def __set__(self, obj, value):11 if value <= 0:12 raise ValueError(f"{self.private_name[1:]} must be positive, got {value}")13 setattr(obj, self.private_name, value)14 15class Product:16 price = Positive()17 stock = Positive()18 19 def __init__(self, name: str, price: float, stock: int):20 self.name = name21 self.price = price # goes through Positive.__set__22 self.stock = stock23 24class PluginMeta(type):25 registry: dict[str, type] = {}26 27 def __new__(mcls, name, bases, namespace):28 cls = super().__new__(mcls, name, bases, namespace)29 mcls.registry[name] = cls30 return cls31 32class Exporter(metaclass=PluginMeta):33 pass34 35class CsvExporter(Exporter):36 pass37 38kurta = Product("Cotton kurta", 799, 12)39print(kurta.price) # 79940print(list(PluginMeta.registry)) # ['Exporter', 'CsvExporter']41Product("Broken item", -5, 1) # ValueError: price must be positive, got -51/6__set_name__ learns the attribute name, so one Positive class works for price, stock or anything else.
A reusable validating descriptor and a metaclass that registers every class it creates, similar in spirit to how frameworks discover models and plugins.
Save yourself the pain
Common mistakes
Oops
Storing the value on the descriptor itself with self.value = value.
Do this instead
The descriptor is shared by every instance, so all products would share one price. Store it on obj, as setattr(obj, ...) does above.
Oops
Writing a metaclass just to register subclasses.
Do this instead
__init_subclass__ on a normal base class does the same with far less magic. Keep metaclasses for framework-level needs.
Your mission
Build your own validators
Quick check
Which built-in feature is implemented using descriptors?
Go deeper
- 1Write a descriptor that validates a positive number field.
- 2Use __init_subclass__ for a simple class registry before reaching for a metaclass.
- 3Read how Django models or dataclasses use these features under the hood.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.