Python lambda 返回 None 而不是空字符串
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 if else 结构
use the if else construct
您的情况中的问题是 '' 被视为布尔值 False。布尔('')==假。
您可以使用
The problem in your case that '' is considered as boolean False. bool('') == False.
You can use
问题是Python 将空字符串视为False。当您将 None 传递给您的函数时,它的计算结果为:
哪个(有效地)变成:
then:
和 最后:
一种解决方案是:
如果您知道 x 将是字符串或 None,那么您可以利用 None 也是这样的事实Python 中的 False 值:
The problem is that Python treats the empty string as False. When you pass None to your function, it evaluates to:
which (effectively) becomes:
then:
and finally:
One solution would be:
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:
Python 赋予
and
的优先级高于or
,因此括号位于此处:当传递
None
时,这将变为(True and '' )或无
。 Python 的布尔运算符通过返回一个或另一个参数来工作(因此出现了这个小技巧),因此这会简化为'' 或 None
,最后是None
。这个小技巧源于 Python 2.5 之前的版本,它没有 条件运算符< /a>.您刚刚遇到的警告是,当
True
分支具有False
值时,它不会按预期运行。除非您关心 Python ≤ 2.4,否则只需使用条件运算符。Python gives
and
a higher precedence thanor
, so the parentheses fall here: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 finallyNone
.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 aFalse
value. Unless you’re concerned with Python ≤ 2.4, just use the conditional operator.这里的问题不是 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 whenexpression1
evaluates to False.This is because the whole thing is evaluated in order. If
condition
fails,expression1
is evaluated. If it isTrue
, it is returned, due to short circuit evaluation, hence the expected behaviour. If not,expression2
is returned.''
evaluates to False.尝试短路评估:
Try short-circuit evaluation: