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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2572564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:57:15  来源:igfitidea点击:

Python lambda returning None instead of empty string

pythonlambda

提问by Virgiliu

I have the following lambda function:

我有以下 lambda 函数:

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.

如果它接收 None 作为参数,它应该返回一个空字符串,如果它不是 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.

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

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.

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

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

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

回答by z33m

use the if else construct

使用 if else 构造

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

回答by Yaroslav

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

在您的情况下, '' 被视为布尔值 False 的问题。bool('') == False。您可以使用

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

回答by Daniel Stutzbach

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

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

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:

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

lambda x: x or ''

回答by Josh Lee

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

Pythonand的优先级高于or,所以括号在这里:

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.

当通过时None,这变为(True and '') or None。Python 的布尔运算符通过返回一个或另一个参数来工作(这个小技巧由此而来),所以这简化为'' or None,最后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 Truebranch has a Falsevalue. Unless you're concerned with Python ≤ 2.4, just use the conditional operator.

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

回答by miku

Try short-circuit evaluation:

尝试短路评估

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

回答by Oliver Zheng

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

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

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

(condition) and (expression1) or (expression2)大多数情况下意味着(condition) ? (expression1) : (expression2)您所期望的,除非expression1评估为 False。

This is because the whole thing is evaluated in order. If conditionfails, expression1is evaluated. If it is True, it is returned, due to short circuit evaluation, hence the expected behaviour. If not, expression2is returned. ''evaluates to False.

这是因为整个事情是按顺序评估的。如果condition失败,expression1则进行评估。如果是True,则由于短路评估而返回,因此是预期的行为。如果没有,expression2则返回。''评估为 False。