Map list by partial function vs lambda

前端 未结 3 1735
一生所求
一生所求 2021-02-05 11:24

I was wondering whether for most examples it is more \'pythonic\' to use lambda or the partial function?

For example, I might want to apply imap on some list, like add 3

3条回答
  •  醉话见心
    2021-02-05 12:18

    lambda is certainly many times more common. Unless you're doing functional programming in an academic setting, you should probably steer away from functools.

    This is pythonic. No library needed, or even builtins, just a simple generator expression.

    ( x + 3 for x in my_list )
    

    This creates a generator, similar to imap. If you're going to make a list out of it anyway, use a list comprehension instead:

    [ x + 3 for x in my_list ]
    

提交回复
热议问题