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

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

问题描述

我正在尝试找出 Python lambdas.lambda 是在现实生活中应该被遗忘的那些有趣"的语言项目之一吗?

我确信在某些边缘情况下可能需要它,但考虑到它的模糊性,它有可能在未来的版本中被重新定义(我的假设基于它的各种定义)和降低的编码清晰度- 应该避免吗?

这让我想起了 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):返回 x % 3 == 0mult3 = 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 函数可能是写东西的最短方法.

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

    <预><代码>>>>定义转换(n):... 返回 lambda x: x + n...>>>f = 变换(3)>>>f(4)7

    这通常用于创建函数包装器,例如 Python 的装饰器.

  • 使用 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'

  • 按备用键排序

    <预><代码>>>>sorted([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

    >>> sorted([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天全站免登陆