• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] Python __complex__() Magic Method

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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

https://www.sickgaming.net/blog/2021/11/...ic-method/

Forum Jump:

 Users browsing this thread: 1 Guest(s)

 Active Threads News - Video: We Have A Lot Of Feelings ... Last Post: xSicKxBot Yesterday 11:53 PM » Replies: 0 » Views: 14 News - Iconic Rockstar And Actor Meat Lo... Last Post: xSicKxBot Yesterday 11:53 PM » Replies: 0 » Views: 1 (Indie Deal) FREE Last Horizon, Microids... Last Post: xSicKxBot Yesterday 08:25 PM » Replies: 0 » Views: 10 Microsoft - Celebrating 20 years of Trus... Last Post: xSicKxBot Yesterday 08:25 PM » Replies: 0 » Views: 3 Xbox Wire - New EA Play Member Rewards F... Last Post: xSicKxBot Yesterday 08:25 PM » Replies: 0 » Views: 3 News - PS5 Will Outsell Series X|S 2:1 I... Last Post: xSicKxBot Yesterday 08:25 PM » Replies: 0 » Views: 4 [Tut] Pandas get_dummies() – A Simple Gu... Last Post: xSicKxBot Yesterday 04:18 PM » Replies: 0 » Views: 19 Mobile - Genshin Impact’s Tomo – vision,... Last Post: xSicKxBot Yesterday 04:18 PM » Replies: 0 » Views: 5 News - Community Focus: Parris Last Post: xSicKxBot Yesterday 04:18 PM » Replies: 0 » Views: 8 News - Bedrock: BETA 1.18.10.28 Last Post: xSicKxBot Yesterday 04:18 PM » Replies: 0 » Views: 8

 Twitter Tweets by _SickGaming