|
> There are a couple ways to do almost everything in Python; loops or map, Twisted or threads, unittest2 or nose, and so on There are always more than 1 ways to do something, but Python tries to be directive towards the recommended way for the core. For the given snippet, using reduce for loops is frowned upon; nothing is stopping you from doing it, but the general opinion is you shouldn't do it. I think that counts as there being an obvious way. def pipe(val, fns):
return reduce(lambda val, fn: fn(val), fns, val)
def pipe2(val, fns):
for fn in fns:
val = fn(val)
return val
fns = [lambda x: x + 1, lambda x: x * x]
print pipe(5, fns)
print pipe2(5, fns)
As far as maps and list comprehension go, that isn't clear-cut but people incline towards comprehensions.<nitpick>
Twisted(reactors) and threads implement two different things which serve different purposes, and unittest2 is a lib while nose is a test runner.
</nitpick> That said, there is always going to be many ways to do something, but it helps if the core tries to stick with a uniform way to do things. |