dot operations

Dan Bishop danb_83 at yahoo.com
Fri Jan 12 04:44:06 CET 2007


On Jan 11, 10:21 am, "Steven W. Orr" <ste... at syslang.net> wrote:
> On Thursday, Jan 11th 2007 at 11:41 +0100, quoth robert:
>
> =>jm.sur... at no.spam.gmail.com wrote:=>> Hi,
> =>>  Frequently I get to do like this:
> =>> a = (1, 2, 3, 4) # some dummy values
> =>> b = (4, 3, 2, 1)
> =>> import operator
> =>> c = map(operator.add, a, b)
> =>>
> =>> I am finding the last line not very readable especially when I combine
> =>> couple of such operations into one line. Is it possible to overload
> =>> operators, so that, I can use .+ for element wise addition, as,
> =>> c = a .+ b
> =>> which is much more readable.
> =>>
> =>> Similarly, I want to use .- , .*, ./   . Is it possible to do?
> =>
> =>import numpy
> =>
> =>You'll not even need dots
>
> I'm very new so my plea to be gentle is still on.
>
> I just looked at the numpy package. Seems very cool, but for the life of
> me I didn't understand the method by which python allows for creation of
> infix operators. Can someone please explain or point me to a reference?

Python doesn't allow the creation of new operators, but you can
overload the existing ones (except for "and" and "or").  This is done
by implementing the methods __add__, __sub__, __mul__, etc.

http://docs.python.org/ref/specialnames.html




More information about the Python-list mailing list