Python Glossary Русский

all Python 3typesterm

types.MemberDescriptorType

descriptors and C wrappers

The type of a slot attribute: __slots__ in your own class creates one, and it also describes C struct fields such as timedelta.days.

Syntax

isinstance(obj, types.MemberDescriptorType)

Each name in __slots__ becomes a MemberDescriptorType in the class __dict__ — it stores the value in a fixed slot of the instance instead of a dict. Hence a trap: until the attribute is assigned, reading it raises AttributeError although the name is "declared". And another one: a class with __slots__ that lacks '__weakref__' does not support weak references.

Examples

import types
class Point:
    __slots__ = ('x', 'y')
print(isinstance(Point.__dict__['x'], types.MemberDescriptorType))  # → True
p = Point()
try:
    p.x
except AttributeError:
    print('AttributeError')  # → AttributeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy