What functional tools remain in Python 3k?

I read several articles on how to remove some functional functions from future python, including map and reduce.

What is the official policy for functional extensions?
lambda function will stay?

+4
source share
2 answers

Well, Python 3.0 and 3.1 are already released, so you can check it out yourself. The end result was that the map and filter were saved as built-in modules, as well as lambda. The only change was that the reduction was moved to the functools module; you just need to do

from functools import reduce 

to use it.

Future 3.x releases are expected to remain backward compatible with 3.0 and 3.1 in this regard.

+10
source

In Python 3.x, Python still has a rich set of functional tools built into: lists, generator expressions, iterators and generators, as well as functions like any() and all() that have a short circuit rating where it is perhaps.

Python's “Benevolent Dictator for Life” recreated the idea of ​​removing map() because you can trivially reproduce its list effects:

 lst2 = map(foo, lst) lst3 = [foo(x) for x in lst] lst2 == lst3 # evaluates True 

The Python lambda function has not been removed or renamed and will probably never be. However, it will probably never become more powerful. Python lambda limited to one expression; it cannot include statements and cannot include multiple lines of Python code.

Python plain-old-standard def defines a function object that can be passed as easily as a lambda object. You can even undo a function name after defining it if you really want to.

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 can actually use the “triple operator” in lambda , so the above example is a bit contrived.

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

But some multi-line functions are hard to insert into lambda and are better handled like simple regular multi-line functions.

Python 3.x has not lost any functional power. There is a general feeling that lists and generator expressions are probably preferable to map() ; in particular, generator expressions can sometimes be used to execute the equivalent of a map() , but without highlighting the list and then releasing it. For instance:

 total = sum(map(lst, foo)) total2 = sum(foo(x) for x in lst) assert total == total2 # same result 

In Python 2.x, map() allocates a new list, which is summarized and immediately freed. The generator expression takes values ​​one at a time and never links the memory of an entire list of values.

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

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

It’s easier to just write a list comprehension!

 lst2 = [x**2 if foo(x) else x for x in lst] 
+3
source