Python Glossary Русский

all Python 3typesterm

types.GetSetDescriptorType

descriptors and C wrappers

The type of a computed attribute of a C class — the analogue of property: for example FunctionType.__dict__['__code__']. The partner of MemberDescriptorType.

Syntax

isinstance(obj, types.GetSetDescriptorType)

These attributes look like plain fields (func.__name__, func.__code__), but they are read and written by C functions that may validate the value: you cannot set func.__name__ = 5 — TypeError. The difference from MemberDescriptorType is where the value comes from: getset computes it, member reads a ready field of the struct. In CPython the types differ, but in other Python implementations they may be the same — do not build logic on the difference.

Examples

import types
d = types.FunctionType.__dict__['__name__']
print(isinstance(d, types.GetSetDescriptorType))  # → True
def f():
    pass
try:
    f.__name__ = 5
except TypeError:
    print('TypeError')  # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy