Introducing Python to others

Paddy O'Loughlin patrick.oloughlin at
Thu Mar 26 19:51:11 CET 2009

Thanks for all your replies.
A lot of very strong answers :)

2009/3/26 Mensanator <mensanator at>:
> What would you have to do to make this work?
>>>> x+x+x      # expecting [3,6]
> [2, 4, 1, 2]

What's happening is that the call to map() is returning a list object.
So after it calculates the first "x+x", you are left with the equivalent of:
[6, 6] + x
Because the list object is on the left, it's __add__() function is
called, which appends x to [6,6].

Instead, convert the list returned by map to a Vector before returning
it. Like so:
>>> class Vector(list):
...     def __add__(self, other):
...             return Vector(map(add, self, other))
>>> x = Vector([3,3])
>>> x+x+x
[9, 9]

"Ray, when someone asks you if you're a god, you say YES!"

More information about the Python-list mailing list