implementation of "complex" type

cfbolz at googlemail.com cfbolz at googlemail.com
Thu Mar 9 07:37:44 EST 2006


Hi!

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.

In CPython it is actually written in C, implemented in the
Objects/complexobject.c file. See for example:

http://svn.python.org/view/python/trunk/Objects/complexobject.c?rev=42596&view=markup

In PyPy it is indeed a pure Python implementation right now:

http://codespeak.net/svn/pypy/dist/pypy/module/__builtin__/app_complex.py

Cheers,

Carl Friedrich Bolz




More information about the Python-list mailing list