Special Method Protocols
__set_name__() — attributes protocol
Receive the owning class and attribute name when type creates a class containing this object. Assignment after class creation does not automatically call the hook. The object need not be a descriptor.
Formal Syntax & Structure
python syntaxpython
def __set_name__(self, owner, name):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| owner | type | New owning class. |
| name | str | Class attribute name. |
Return Value
None
Notification completes without a result.
Working Example
Example Snippetpython
class Field: def __set_name__(self, owner, name): self.name = nameclass Record: price = Field()print(Record.price.name)