Python Glossary Русский

Python 3.4+typesterm

types.DynamicClassAttribute

descriptors and C wrappers

A descriptor that acts like property on an instance but sends class-level lookup to the metaclass __getattr__. This is how enum gives members their name and value.

Syntax

@types.DynamicClassAttribute
def name(self): ...

The problem it solves: an instance and its class need an attribute of the same name with different meanings. A plain property accessed on the class would return the property object itself; DynamicClassAttribute raises AttributeError instead, so Python falls back to the metaclass __getattr__. Without such a metaclass, class-level access simply fails. Everyday code rarely needs it — you meet it reading the enum source.

Examples

import types
class Item:
    @types.DynamicClassAttribute
    def kind(self):
        return 'instance'
print(Item().kind)  # → instance
try:
    Item.kind
except AttributeError:
    print('AttributeError')  # → AttributeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy