为什么Python lambdas有用? [英] Why are Python lambdas useful?

查看:87
本文介绍了为什么Python lambdas有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚Python lambdas。是在现实生活中应该忘记的那些有趣的语言项目之一?



我确定有一些边缘情况可能需要它,但鉴于它的晦涩,它的潜力在未来版本中重新定义(我的假设对它的各种定义)和减少的编码清晰度 - 应该避免?



这让我想起C类型的溢出(缓冲区溢出) - 指向顶部变量,并重载以设置其他字段值。

解决方案

你在谈论lambda函数吗?喜欢

  lambda x:x ** 2 + 2 * x  -  5 

这些东西其实很有用。 Python支持一种称为函数式编程的编程风格,其中你可以将函数传递给其他函数来做事情。示例:

  mult3 = filter(lambda x:x%3 == 0,[1,2,3,4,5 ,6,7,8,9])

设置 mult3 [3,6,9] ,原始列表的那些元素是3的倍数。这更短(并且,可以争辩,更清楚)

  def filterfunc(x):
return x%3 == 0
mult3 = filter filterfunc,[1,2,3,4,5,6,7,8,9])

当然,在这种特殊情况下,你可以做一个列表解析相同的事情:

  mult3 = [x for x in [1,2,3,4,5,6,7,8,9] if x%3 == 0] 

(或者甚至作为 range(3,10,3)),但有很多其他更复杂的用例,你不能使用列表推导和lambda函数可能是写出东西的最短方式。




  • 从另一个函数返回一个函数



     >>> def transform(n):
    ... return lambda x:x + n
    ...
    >>> f = transform(3)
    >>>> f(4)
    7

    这通常用于创建函数包装器,


  • 将可迭代序列的元素与 reduce()

     >>> reduce(lambda a,b:'{},{}'。format(a,b),[1,2,3,4,5,6,7,8,9])
    ' ,3,4,5,6,7,8,9'


  • 备用键

     >>> sort([1,2,3,4,5,6,7,8,9],key = lambda x:abs(5-x))
    [5,4,6,3,7,2 ,8,1,9]




lambda函数。我花了一段时间来习惯他们,但最终我明白他们是这门语言中非常有价值的一部分。


I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten?

I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it being redefined in future releases (my assumption based on the various definitions of it) and the reduced coding clarity - should it be avoided?

This reminds me of overflowing (buffer overflow) of C types - pointing to the top variable and overloading to set the other field values. It feels like sort of a techie showmanship but maintenance coder nightmare.

解决方案

Are you talking about lambda functions? Like

lambda x: x**2 + 2*x - 5

Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff. Example:

mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])

sets mult3 to [3, 6, 9], those elements of the original list that are multiples of 3. This is shorter (and, one could argue, clearer) than

def filterfunc(x):
    return x % 3 == 0
mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Of course, in this particular case, you could do the same thing as a list comprehension:

mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]

(or even as range(3,10,3)), but there are many other, more sophisticated use cases where you can't use a list comprehension and a lambda function may be the shortest way to write something out.

  • Returning a function from another function

    >>> def transform(n):
    ...     return lambda x: x + n
    ...
    >>> f = transform(3)
    >>> f(4)
    7
    

    This is often used to create function wrappers, such as Python's decorators.

  • Combining elements of an iterable sequence with reduce()

    >>> reduce(lambda a, b: '{}, {}'.format(a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9])
    '1, 2, 3, 4, 5, 6, 7, 8, 9'
    

  • Sorting by an alternate key

    >>> sort([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))
    [5, 4, 6, 3, 7, 2, 8, 1, 9]
    

I use lambda functions on a regular basis. It took me a while to get used to them, but eventually I came to understand that they're a very valuable part of the language.

这篇关于为什么Python lambdas有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆