English 中文(简体)
what functional tools remain in Python 3k?
原标题:

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

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]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签