Python Glossary Русский

Python 3.7+typesterm

types.WrapperDescriptorType

descriptors and C wrappers

The type of slot wrappers of built-in classes such as object.__init__ or int.__add__: a special method in C taken from the class, not from an instance.

Syntax

isinstance(obj, types.WrapperDescriptorType)

Print int.__add__ and Python shows <slot wrapper '__add__' of 'int' objects> — that is a WrapperDescriptorType. It is neither FunctionType nor MethodType, so a decorator or check that lists those types will miss it. Taken from an instance ((1).__add__) it becomes a MethodWrapperType, just as a class function becomes a method. To recognise any routine, use inspect.isroutine().

Examples

import types
print(isinstance(int.__add__, types.WrapperDescriptorType))  # → True
print(isinstance((1).__add__, types.MethodWrapperType))  # → True
print(int.__add__(2, 3))  # → 5

See also

Open in the full glossarydocs.python.orgReport an inaccuracy