Python 3.14+Numbers & mathfunction
complex.from_number()
complex numbers
Class method that builds a complex number from a single numeric argument (delegates to __complex__, then __float__, then __index__). Unlike complex(), it does not accept strings. Python 3.14+.
Syntax
classmethod complex.from_number(x)
The method landed in 3.14, so on older interpreters — graders included — touching it raises AttributeError; plain complex(x) stays the portable choice. Its whole point is strictness: exactly one numeric argument, and a string gets a TypeError, whereas complex('1+2j') happily parses text — handy for user input, dangerous when you expected a number.
Examples
print(complex.from_number(3)) # → (3+0j)
print(complex.from_number(2.5)) # → (2.5+0j)
print(complex.from_number(1+2j)) # → (1+2j)
print(complex.from_number(True)) # → (1+0j)
try: complex.from_number('1+2j') # строка не число
except TypeError as e: print(type(e).__name__) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy