complex
Monty implements the complex type: imaginary literals (2j), the complex() constructor including its string
form, arithmetic with CPython 3.14’s mixed-mode rules, abs(), == and hash() across the numeric tower, the
real, imag and conjugate() members, complex.from_number(), and the format mini-language.
A complex crosses the host boundary as a Python complex and as a { __monty_type__: 'Complex', real, imag }
marker in JavaScript.
The divergences are the ones the rest of the numeric tower already has.
complex(obj)on a class instance never calls__complex__,__float__or__index__; it raisesTypeError: complex() argument must be a string or a number, not X, asfloat()andint()do (see classes.md).- Non-ASCII digits in the string form are rejected:
complex('١j')raisesValueError: complex() arg is a malformed string, where CPython accepts any Unicode decimal digit. This is the same restriction asint()andfloat()(see builtins.md).
z.conjugateas a value (without calling it) raisesAttributeError; only the callz.conjugate()works. Builtin methods are not first-class values in Monty (see builtins.md).complex.real,complex.imagandcomplex.conjugateon the type raiseAttributeError; CPython returns descriptors.real,imagandconjugate()onintandfloatraiseAttributeError; onlycomplexcarries them.- Dunder methods such as
z.__abs__()orz.__complex__()raiseAttributeError; useabs(z)andcomplex(z).
hash(z)differs from CPython’s value unlesszequals aboolor a smallint, because the parts hash with Monty’s float algorithm (see builtins.md); it always agrees with the hash of an equalintorfloat, so mixed-type dict keys behave.- Repeating a sequence by a complex (
[1] * 1j,1j * 'a',(1,) * 1j) raisesTypeError: unsupported operand type(s) for *: 'list' and 'complex'naming the two operand types; CPython sayscan't multiply sequence by non-int of type 'complex'(see collections.md).