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?
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?
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.
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
In short, Python 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)
total == total2 # evaluates True
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.