Special Method Protocols

__complex__() — numeric protocol

Provide the complex conversion. Without this hook, complex() can try float and index conversions. The hook must return the specified numeric type.

Formal Syntax & Structure

python syntaxpython
def __complex__(self):

Return Value

complex

Converted numeric value.

Working Example

Example Snippetpython
import operatorclass Number:    def __init__(self, value):        self.value = value    def __complex__(self):        return complex(self.value, 2)print(complex(Number(3)))
Python Reference