Python __complex__() Magic Method

Syntax

object.__complex__(x)

The Python __complex__() method implements the built-in complex() function. So, when you cal complex(x), Python attempts to call x.__complex__(). If the return value is not a complex number or the x.__complex__() method is not defined for an object on which you call complex(x), Python will raise a TypeError.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Background complex()

Python’s built-in complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.

Here are some examples:

>>> complex(1, -2)
(1-2j)
>>> complex(2, -1)
(2-1j)
>>> complex(2, 2)
(2+2j)
>>> complex(1)
(1+0j)
>>> complex(2)
(2+0j)
>>> complex('42-21j')
(42-21j)

Example Custom __complex__()

In the following example, you create a custom class Data and overwrite the __complex__() magic method so that it returns a complex number (42+21j) when trying to call complex(x) on a custom Data object.

class Data:
    def __complex__(self):
        return (42+21j)


x = Data()
res = complex(x) 

print(res)
# (42+21j)

TypeError: complex() first argument must be a string or a number, not …

If you hadn’t defined the __complex__() magic method, Python would’ve raised a TypeError:

class Data:
    pass


x = Data()
res = complex(x)

print(res)

Output:

Traceback (most recent call last):
  File "C:\Users\xcent\Desktop\code.py", line 6, in <module>
    res = complex(x)
TypeError: complex() first argument must be a string or a number, not 'Data'

To fix this error, define the x.__complex__() method for an object x before calling the built-in complex(x) method passing this object as an argument:

class Data:
    def __complex__(self):
        return 1+2j


x = Data()
res = complex(x)

print(res)
# (1+2j)

TypeError: __bool__ should return bool, returned …

Consider the following code snippet where you try to return an integer, i.e., non-complex number in the dunder method __complex__():

class Data:
    def __complex__(self):
        return 42


x = Data()
res = complex(x)

print(res)

Running this leads to the following error message on my computer:

Traceback (most recent call last):
  File "C:\Users\xcent\Desktop\code.py", line 7, in <module>
    res = complex(x)
TypeError: __complex__ returned non-complex (type int)

The reason for the TypeError: __complex__ returned non-complex (type ...) error is that the __complex__() method must return a complex number. So, to resolve the error, return a complex number, for example, by using the syntax x+yj for two integers x and y in your method definition as shown previously:

class Data:
    def __complex__(self):
        return 1+2j # This is a complex number


x = Data()
res = complex(x)

print(res)
# (1+2j)

References:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!