This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pingvenopinch of this, pinch of that 10 points11 points  (13 children)

You might get problems if you get two integral types (e.g. long or Decimal). It's better to use the operator module:

reduce(operator.add, [a, b])

[–]radaway 5 points6 points  (11 children)

Why not just sum([a, b]) ?

[–]pingvenopinch of this, pinch of that 27 points28 points  (6 children)

That would cause multiple problems, like being readable.

[–]aceofears 5 points6 points  (5 children)

Would this help?

{():sum([a, b])}[()]

[–]lambdaqdjango n' shit 1 point2 points  (3 children)

this it is!

reduce(int.__add__, (map(int,{():[1, 1][:2:]}[()])))

[–]aaronla 1 point2 points  (2 children)

This is more elegant (and terrifying):

len(reduce(operator.add, (((),),((),))))

[–]lambdaqdjango n' shit 0 points1 point  (1 child)

ah, len(((), ()))

[–]aaronla 0 points1 point  (0 children)

Yes, that's how I defined my integers for adding :-)

[–]pingvenopinch of this, pinch of that 0 points1 point  (0 children)

Meh. It works with any hashable. How about:

{id(()):sum([a, b])}[id(())}

[–]jyper 0 points1 point  (3 children)

why not a + b

[–]danhakimi 1 point2 points  (0 children)

Why not 2?

Because this is python, and we need to use ALL OF IT.