implementation of "complex" type

Steven D'Aprano steve at
Thu Mar 9 12:19:46 CET 2006

On Thu, 09 Mar 2006 01:15:57 -0800, Russ wrote:

> I tried the following:
>>>> x = complex(4)
>>>> y = x
>>>> y *= 2
>>>> print x, y
> (4+0j) (8+0j)
> But when I tried the same thing with my own class in place of
> "complex" above, I found that both x and y were doubled. I'd like to
> make my class behave like the "complex" class. Can someone tell me the
> trick? Also, where can I find the code for for the "complex" class? I
> hope it's written in Python! Thanks.

Or, to put it another way:

"Here's some code that works!

Now, I've written some *other* code, which has a bug in it. Who'd like to
guess what the bug is?"

Why don't you show us your complex class?

Also, why are you re-inventing the wheel? Is it just to learn the
language? Or because you want to add extra functionality? Or just NIH
Syndrome? If it is a learning exercise, they I applaud you; if it is to
add extra functionality, there is a better way.


More information about the Python-list mailing list