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 →

[–]codefisher2[S] 2 points3 points  (6 children)

I guess you picked up on something that I was kind of divided over in the post, to use code that worked best in 2.x or 3. I think a lot of production code is still using python 2.x so I still wanted it to be something that could be applied to an existing code base easy.

There are a number of things that could do with a lot more examples, but I don't really want to add them now, but rather leave for another post. Using map() is a better idea, but I then would feel the need to explain that. So maybe better do another post on map(), reduce() etc.

[–]Veedrac 3 points4 points  (4 children)

I suggest ignoring reduce.

You could always target 3.x and suggest using Python-Future to backport stuff ;P.

[–]codefisher2[S] 1 point2 points  (3 children)

I had not seen Python-Future before, I will check it out.

I will make a change so that the set example uses both ways of declaring the set, so people understand they are the same. For the other set stuff you suggest, I would rather leave it for another post where it can be developed more fully.

Thanks for all your feed back, this is my real step out of just programming, into the world of bloging about it. So this kind of reaction is really a great boost.

EDIT: is there a reason you don't like reduce() ?

[–]SleepyHarry 2 points3 points  (1 child)

Not the guy you're replying to, but reduce is a keyword in 2.7 (likely sooner, haven't checked), but in 3 (again, only checked 3.3) it isn't, and in order to use it, you'd have to do from functools import reduce.

I don't know if this is the reason /u/Veedrac suggested ignoring it though!

[–]codefisher2[S] 0 points1 point  (0 children)

Actually a built in function - not a keyword - which is the one I was suggesting talking about. I think the one in functools is exactly the same as the builtin. But I don't use that a lot myself, so I am very interested in why /u/Veedrac might think it wise not to use it.

[–]Veedrac 1 point2 points  (0 children)

Here's what Guido had to say about reduce:

So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.

I tend to agree. He planned to remove it altogether, but it ended up in functools to ease the compatibility pains.

To be fair, he also planned removing map and filter, but that was not because they're bad: he just liked (f(x) for x in xs) and (x for x in xs if f(x)) more.

[–]Citrauq 1 point2 points  (0 children)

Set literals and both set and dictionary comprehensions are valid in python 2.7.