Python lambda 返回 None 而不是空字符串

发布于 2024-08-27 06:04:43 字数 419 浏览 5 评论 0原文

我有以下 lambda 函数:

f = lambda x: x == None and '' or x

如果它接收 None 作为参数,它应该返回一个空字符串,如果它不是 None,则应该返回参数。

例如:

>>> f(4)
4
>>> f(None)
>>>

如果我调用 f(None) 而不是得到一个空字符串,我会得到 None。我打印了函数返回的类型,得到了 NoneType。我期待着字符串。

type('') 返回字符串,所以我想知道为什么当我传递 None 作为参数时 lambda 不返回空字符串。

我对 lambda 相当陌生,所以我可能误解了它们如何工作的一些事情。

I have the following lambda function:

f = lambda x: x == None and '' or x

It should return an empty string if it receives None as the argument, or the argument if it's not None.

For example:

>>> f(4)
4
>>> f(None)
>>>

If I call f(None) instead of getting an empty string I get None. I printed the type of what the function returned and I got NoneType. I was expecting string.

type('') returns string, so I'd like to know why the lambda doesn't return an empty string when I pass None as an argument.

I'm fairly new to lambdas so I might have misunderstood some things about how they work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街道布景 2024-09-03 06:04:43

使用 if else 结构

f = lambda x:'' if x is None else x

use the if else construct

f = lambda x:'' if x is None else x
草莓酥 2024-09-03 06:04:43

您的情况中的问题是 '' 被视为布尔值 False。布尔('')==假。
您可以使用

f =lambda x:x if x is not None else ''

The problem in your case that '' is considered as boolean False. bool('') == False.
You can use

f =lambda x:x if x is not None else ''
挽清梦 2024-09-03 06:04:43

问题是Python 将空字符串视为False。当您将 None 传递给您的函数时,它的计算结果为:

None == None and '' or None

哪个(有效地)变成:

True and False or None

then:

False or None

和 最后:

None

一种解决方案是:

lambda x: x if x is not None else ''

如果您知道 x 将是字符串或 None,那么您可以利用 None 也是这样的事实Python 中的 False 值:

lambda x: x or ''

The problem is that Python treats the empty string as False. When you pass None to your function, it evaluates to:

None == None and '' or None

which (effectively) becomes:

True and False or None

then:

False or None

and finally:

None

One solution would be:

lambda x: x if x is not None else ''

If you know x will be either a string or None, then you can leverage the fact that None is also a False value in Python:

lambda x: x or ''
远昼 2024-09-03 06:04:43

Python 赋予 and 的优先级高于 or,因此括号位于此处:

lambda x: (x == None and '') or x

当传递 None 时,这将变为 (True and '' )或无。 Python 的布尔运算符通过返回一个或另一个参数来工作(因此出现了这个小技巧),因此这会简化为 '' 或 None,最后是 None

这个小技巧源于 Python 2.5 之前的版本,它没有 条件运算符< /a>.您刚刚遇到的警告是,当 True 分支具有 False 值时,它不会按预期运行。除非您关心 Python ≤ 2.4,否则只需使用条件运算符。

Python gives and a higher precedence than or, so the parentheses fall here:

lambda x: (x == None and '') or x

When passed None, this becomes (True and '') or None. Python’s boolean operators work by returning one argument or another (whence this little trick), so this reduces to '' or None, and finally None.

This little trick stems from back before Python 2.5, which didn't have the conditional operator. The caveat, which you just ran into, is that it doesn’t behave as expected when True branch has a False value. Unless you’re concerned with Python ≤ 2.4, just use the conditional operator.

梦中楼上月下 2024-09-03 06:04:43

这里的问题不是 lambda。这是您在那里使用的 pythonic if/else 表达式。

(条件) 和 (表达式1) 或(表达式2) 大多数时候意味着(条件) ? (表达式1) : (表达式2) 如您所料,除非表达式1 的计算结果为False。

这是因为整个事情是按顺序评估的。如果条件失败,则计算表达式1。如果它是True,则由于短路评估而返回,因此是预期的行为。如果不是,则返回表达式2'' 的计算结果为 False。

It's not lambdas that are the problem here. It's the pythonic if/else expressiong you are using there.

(condition) and (expression1) or (expression2) most of the times means the (condition) ? (expression1) : (expression2) you'd expect, except when expression1 evaluates to False.

This is because the whole thing is evaluated in order. If condition fails, expression1 is evaluated. If it is True, it is returned, due to short circuit evaluation, hence the expected behaviour. If not, expression2 is returned. '' evaluates to False.

困倦 2024-09-03 06:04:43

尝试短路评估

 >>> g = lambda x: x or ''
 >>> g(3)
 3
 >>> g(None)
 ''
 >>> # beware that ...
 >>> g(0)
 ''

Try short-circuit evaluation:

 >>> g = lambda x: x or ''
 >>> g(3)
 3
 >>> g(None)
 ''
 >>> # beware that ...
 >>> g(0)
 ''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文