Google Python style guide

前端 未结 4 938
南旧
南旧 2020-11-30 01:44

Why does the Google Python Style Guide prefer list comprehensions and for loops instead of filter, map, and reduce?

Deprecated Language Features: ... \"Use list com

4条回答
  •  醉话见心
    2020-11-30 01:49

    map and filter are way less powerful than their list comprehension equivalent. LCs can do both filtering and mapping in one step, they don't require explicit function and can be compiled more efficiently because of their special syntax

    # map and filter
    map(lambda x:x+1, filter(lambda x:x%3, range(10)))
    # same as LC
    [x+1 for x in range(10) if x%3]
    

    There is simply no reason to prefer map or filter over LCs.

    reduce is slightly different, because there is no equivalent LC, but it has no big advantage over a ordinary for-loop either.

提交回复
热议问题