Special Method Protocols
__set__() — attributes protocol
Implement descriptor writes. A descriptor with __set__ is a data descriptor and takes precedence over a same-named instance dictionary entry. Store the value under a separate name.
Formal Syntax & Structure
python syntaxpython
def __set__(self, instance, value):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| instance | object | Receiving instance. |
| value | object | Assigned value. |
Return Value
None
Assignment has no meaningful result.
Working Example
Example Snippetpython
class Field: def __get__(self, instance, owner=None): if instance is None: return self return instance._count def __set__(self, instance, value): if value < 0: raise ValueError("negative") instance._count = valueclass Record: count=Field()def demo(): r=Record(); r.count=5 return r.countprint(demo())