complex numbers
Antoon Pardon
apardon at forel.vub.ac.be
Wed Jan 12 03:07:52 EST 2005
Op 2005-01-12, It's me schreef <itsme at yahoo.com>:
>
> "Robert Kern" <rkern at ucsd.edu> wrote in message
> news:cs1mp9$sg9$1 at news1.ucsd.edu...
>>
>> That's *it*.
>
> So, how would you overload an operator to do:
>
> With native complex support:
>
> def twice(a):
> return 2*a
>
> print twice(3+4j), twice(2), twice("abc")
>
> Let's presume for a moment that complex is *not* a native data type in
> Python. How would we implement the above - cleanly?
I suppose in the same way as (graphic) points and vectors can be
implemented cleanly.
A few years back I had written a Vector class in python, just
to get an understanding of how things worked. It worked without
a problem with your twice function.
>>> Vec(1.0,2.0)
Vector[1.0, 2.0]
>>> def twice(a):
... return 2 * a
...
>>> twice(Vec(1.0,2.0))
Vector[2.0, 4.0]
>>>
I suppose what can be done with a vector class could have been
done with a complex class should complex numbers not have been
native to python.
--
Antoon Pardon
More information about the Python-list
mailing list