Python Glossary Русский

Python 3.7+typesterm

types.MethodWrapperType

descriptors and C wrappers

The type of a built-in class's special method already bound to an instance: (1).__add__, object().__str__. The bound partner of WrapperDescriptorType.

Syntax

isinstance(obj, types.MethodWrapperType)

This is the bound method for C slot methods: self is already filled in, so (1).__add__(2) gives 3. The trap is to confuse it with MethodType: isinstance((1).__add__, types.MethodType) is False, although it is a bound method in meaning. You meet it when a special method of an instance is passed to map or sorted. Checking "can I call it" is safer with callable().

Examples

import types
add_one = (1).__add__
print(isinstance(add_one, types.MethodWrapperType))  # → True
print(isinstance(add_one, types.MethodType))  # → False
print(list(map(add_one, [10, 20])))  # → [11, 21]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy