Question

I have have read several entries regarding dropping several functional functions from future python, including map and reduce.

What is the official policy regarding functional extensions?
is lambda function going to stay?

Was it helpful?

Solution

Well, Python 3.0 and 3.1 are already released, so you can check this out for yourself. The end result was that map and filter were kept as built-ins, and lambda was also kept. The only change was that reduce was moved to the functools module; you just need to do

from functools import reduce

to use it.

Future 3.x releases can be expected to remain backwards-compatible with 3.0 and 3.1 in this respect.

OTHER TIPS

In Python 3.x, Python continues to have a rich set of functional-ish tools built in: list comprehensions, generator expressions, iterators and generators, and functions like any() and all() that have short-circuit evaluation wherever possible.

Python's "Benevolent Dictator For Life" floated the idea of removing map() because you can trivially reproduce its effects with a list comprehension:

lst2 = map(foo, lst)

lst3 = [foo(x) for x in lst]

lst2 == lst3  # evaluates True

Python's lambda feature has not been removed or renamed, and likely never will be. However, it will likely never become more powerful, either. Python's lambda is restricted to a single expression; it cannot include statements and it cannot include multiple lines of Python code.

Python's plain-old-standard def defines a function object, which can be passed around just as easily as a lambda object. You can even unbind the name of the function after you define it, if you really want to do so.

Example:

# NOT LEGAL PYTHON
lst2 = map(lambda x: if foo(x): x**2; else: x, lst)

# perfectly legal Python
def lambda_function(x):
    if foo(x):
        return x**2
    else:
        return x

lst2 = map(lambda_function, lst)

del(lambda_function) # can unbind the name if you wish

Note that you could actually use the "ternary operator" in a lambda so the above example is a bit contrived.

lst2 = map(lambda x: x**2 if foo(x) else x, lst)

But some multiline functions are difficult to force into a lambda and are better handled as simple ordinary multiline functions.

Python 3.x has lost none of its functional power. There is some general feeling that list comprehensions and generator expressions are probably preferable to map(); in particular, generator expressions can sometimes be used to do the equivalent of a map() but without allocating a list and then freeing it again. For example:

total = sum(map(lst, foo))

total2 = sum(foo(x) for x in lst)

assert total == total2  # same result

In Python 2.x, the map() allocates a new list, which is summed and immediately freed. The generator expression gets the values one at a time and never ties up the memory of a whole list of values.

In Python 3.x, map() is "lazy" so both are about equally efficient. But as a result, in Python 3.x the ternary lambda example needs to be forced to expand into a list:

lst2 = list(map(lambda x: x**2 if foo(x) else x, lst))

Easier to just write the list comprehension!

lst2 = [x**2 if foo(x) else x for x in lst]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top