Python Glossary Русский

Python 3.5+typingterm

typing.SupportsComplex

Supports* protocols

A runtime-checkable protocol for objects supporting complex() (has __complex__).

Syntax

isinstance(obj, typing.SupportsComplex)

Gotcha: int and float have no __complex__ — complex(5) and complex(2.0) work through __index__/__float__, yet isinstance(5, SupportsComplex) is False. The protocol fires only for types that define __complex__ explicitly (complex itself, your own classes), not for everything complex() accepts.

Examples

# Python 3.11+
from typing import SupportsComplex
print(isinstance(3j, SupportsComplex))   # → True
print(isinstance(5, SupportsComplex))  # → False
print(isinstance('3+4j', SupportsComplex))  # → False
from typing import SupportsComplex
class Vec: __complex__ = lambda self: 1+2j
print(isinstance(Vec(), SupportsComplex))  # → True
print(complex(Vec()))  # → (1+2j)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy