404

在 C# 中有一个null 合并运算符(写为??),它允许在分配期间轻松(短)检查 null:

string s = null;
var other = s ?? "some default value";

有python等价物吗?

我知道我可以做到:

s = None
other = s if s else "some default value"

但是有没有更短的方法(我不需要重复s)?

4

11 回答 11

557
other = s or "some default value"

好的,必须弄清楚or运营商是如何工作的。它是一个布尔运算符,因此它在布尔上下文中工作。如果这些值不是布尔值,则将它们转换为布尔值以供运算符使用。

请注意,or运算符不只返回Trueor False。相反,如果第一个操作数的计算结果为真,则返回第一个操作数,如果第一个操作数的计算结果为假,则返回第二个操作数。

在这种情况下,如果表达式为 true,则表达式x or y返回或在转换为布尔值时计算为 true。否则,它返回. 在大多数情况下,这将用于 C♯ 的空合并运算符的相同目的,但请记住:xTruey

42    or "something"    # returns 42
0     or "something"    # returns "something"
None  or "something"    # returns "something"
False or "something"    # returns "something"
""    or "something"    # returns "something"

如果您使用变量s来保存对类实例的引用或None(只要您的类没有定义成员__nonzero__()and __len__()),则使用与 null-coalescing 运算符相同的语义是安全的。

事实上,拥有 Python 的这种副作用甚至可能很有用。由于您知道哪些值的计算结果为 false,因此您可以使用它来触发默认值,而无需None专门使用(例如错误对象)。

在某些语言中,这种行为称为Elvis 运算符

于 2011-02-12T15:06:31.923 回答
97

严格来说,

other = s if s is not None else "default value"

否则,s = False将变为"default value",这可能不是预期的。

如果您想缩短此时间,请尝试:

def notNone(s,d):
    if s is None:
        return d
    else:
        return s

other = notNone(s, "default value")
于 2011-02-12T15:52:48.090 回答
51

这是一个函数,它将返回不是的第一个参数None

def coalesce(*arg):
  return reduce(lambda x, y: x if x is not None else y, arg)

# Prints "banana"
print coalesce(None, "banana", "phone", None)

reduce()即使第一个参数不是,也可能不必要地迭代所有参数None,所以你也可以使用这个版本:

def coalesce(*arg):
  for el in arg:
    if el is not None:
      return el
  return None
于 2013-04-27T00:45:21.267 回答
14

如果您需要嵌套多个空合并操作,例如:

model?.data()?.first()

这不是一个容易解决的问题or。它也无法解决.get()需要字典类型或类似类型(并且无论如何都不能嵌套)或者getattr()当 NoneType 没有属性时会引发异常的问题。

考虑在语言中添加空合并的相关 pip 是PEP 505,与文档相关的讨论在python-ideas线程中。

于 2020-04-24T11:18:35.753 回答
11

我意识到这已得到解答,但是当您处理类似 dict 的对象时,还有另一种选择。

如果您有一个对象可能是:

{
   name: {
      first: "John",
      last: "Doe"
   }
}

您可以使用:

obj.get(property_name, value_if_null)

像:

obj.get("name", {}).get("first", "Name is missing") 

通过添加{}为默认值,如果缺少“name”,则返回一个空对象并传递给下一个 get。这类似于 C# 中的 null-safe-navigation,类似于obj?.name?.first.

于 2018-07-13T12:32:44.167 回答
4

除了@Bothwells 对单个值的回答(我更喜欢)之外,为了对函数返回值进行空值检查,您可以使用新的 walrus-operator(从 python3.8 开始):

def test():
    return

a = 2 if (x:= test()) is None else x

因此,test函数不需要被评估两次(如a = 2 if test() is None else test()

于 2020-03-02T13:03:27.537 回答
2

除了朱利亚诺关于“或”行为的回答:它是“快”

>>> 1 or 5/0
1

所以有时它可能是一个有用的捷径,比如

object = getCachedVersion() or getFromDB()
于 2014-06-24T12:24:28.200 回答
0

关于@Hugh Bothwell、@mortehu 和@glglgl 的回答。

设置数据集进行测试

import random

dataset = [random.randint(0,15) if random.random() > .6 else None for i in range(1000)]

定义实现

def not_none(x, y=None):
    if x is None:
        return y
    return x

def coalesce1(*arg):
  return reduce(lambda x, y: x if x is not None else y, arg)

def coalesce2(*args):
    return next((i for i in args if i is not None), None)

制作测试功能

def test_func(dataset, func):
    default = 1
    for i in dataset:
        func(i, default)

使用 python 2.7 在 mac i7 @2.7Ghz 上的结果

>>> %timeit test_func(dataset, not_none)
1000 loops, best of 3: 224 µs per loop

>>> %timeit test_func(dataset, coalesce1)
1000 loops, best of 3: 471 µs per loop

>>> %timeit test_func(dataset, coalesce2)
1000 loops, best of 3: 782 µs per loop

显然,该not_none函数正确回答了 OP 的问题并处理了“虚假”问题。它也是最快和最容易阅读的。如果在许多地方应用逻辑,这显然是最好的方法。

如果您想在可迭代对象中找到第一个非空值时遇到问题,那么@mortehu 的响应就是要走的路。但它解决了与 OP不同的问题,尽管它可以部分处理这种情况。它不能采用可迭代和默认值。最后一个参数将是返回的默认值,但是在这种情况下您不会传入一个可迭代的,并且最后一个参数是默认值也不是明确的。

然后你可以在下面做,但我仍然会使用not_null单值用例。

def coalesce(*args, **kwargs):
    default = kwargs.get('default')
    return next((a for a in arg if a is not None), default)
于 2019-06-07T00:08:55.487 回答
-1

对于像我这样偶然发现此问题的可行解决方案的人,当变量可能未定义时,我得到的最接近的是:

if 'variablename' in globals() and ((variablename or False) == True):
  print('variable exists and it\'s true')
else:
  print('variable doesn\'t exist, or it\'s false')

请注意,在检查全局变量时需要一个字符串,但之后在检查值时使用实际变量。

有关变量存在的更多信息: 如何检查变量是否存在?

于 2020-01-09T16:14:52.220 回答
-3
Python has a get function that its very useful to return a value of an existent key, if the key exist;
if not it will return a default value.

def main():
    names = ['Jack','Maria','Betsy','James','Jack']
    names_repeated = dict()
    default_value = 0

    for find_name in names:
        names_repeated[find_name] = names_repeated.get(find_name, default_value) + 1

如果您在字典中找不到名称,它将返回 default_value,如果名称存在,则它将添加任何现有值与 1。

希望这可以帮助

于 2019-10-18T00:29:48.587 回答
-6

我发现下面的两个函数在处理许多可变测试用例时非常有用。

def nz(value, none_value, strict=True):
    ''' This function is named after an old VBA function. It returns a default
        value if the passed in value is None. If strict is False it will
        treat an empty string as None as well.

        example:
        x = None
        nz(x,"hello")
        --> "hello"
        nz(x,"")
        --> ""
        y = ""   
        nz(y,"hello")
        --> ""
        nz(y,"hello", False)
        --> "hello" '''

    if value is None and strict:
        return_val = none_value
    elif strict and value is not None:
        return_val = value
    elif not strict and not is_not_null(value):
        return_val = none_value
    else:
        return_val = value
    return return_val 

def is_not_null(value):
    ''' test for None and empty string '''
    return value is not None and len(str(value)) > 0
于 2016-03-14T12:37:47.947 回答