简体   繁体   English

Python'和'可以返回无?

[英]can Python 'and' return None?

I am looking at some code which apparently runs, as nobody has complained about it, but am well confused by what they have written: 我正在查看一些显然运行的代码,因为没有人抱怨它,但我对它们所写的内容感到困惑:

if a and b is not None:
    # do something

I have always thought of the 'and' operator as something which returns True or False, now am starting to doubt myself.. What else would it return, a number.. It is probably not pythonic, but am I missing something - how can someone write something like that? 我一直认为'和'运算符是返回True或False的东西,现在我开始怀疑自己..它还会返回什么,一个数字......它可能不是pythonic,但我错过了什么 - 怎么能有人写这样的东西?

It means if a is Truthy and b is not None and not what you thought it meant ie a and b is Truthy 这意味着如果a is Truthyb is not None而不是你认为它意味着a and b is Truthy

a = 999
b = None

if a and b is not None:
    print("a is True but b is None")
else:
    print("a is True and b is not None")

The code above means: 上面的代码意味着:

If a (is truthy), AND b isn't None , then #do something . 如果a (是真的),AND b不是None ,那么#do something

According to [Python 3]: Operator precedence ( emphasis is mine): 根据[Python 3]:运算符优先级强调是我的):

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding) . 下表总结了Python中的运算符优先级, 从最低优先级(最小绑定)到最高优先级(大多数绑定)

 ... and Boolean AND not x Boolean NOT in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests ... 

The fact that is not comes after and , means that it will be evaluated before and (both might not be evaluated at all, due to lazy evaluation - thanks @NickA for the comment), so the expression is equivalent to (adding parentheses for clarity): 事实并非如此 并且意味着它将在之前进行评估并且 (由于懒惰的评估 ,可能根本不进行评估 - 感谢@NickA的评论),因此表达式相当于(为了清晰起见,添加了括号) ):

if a and (b is not None):

Also, according to [Python 3]: Truth Value Testing : 另外,根据[Python 3]:真值测试

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. 可以测试任何对象的真值,用于ifwhile条件或下面的布尔运算的操作数。

your if statement is perfectly OK (produces a bool ). 你的if语句是完全OK(产生布尔 )。

Examples (using [Python 3]: class bool ( [x] ) ): 示例(使用[Python 3]:类bool[x] ):

 >>> bool(0) False >>> bool(100) True >>> bool([]) False >>> bool([0]) True >>> bool(None) False >>> bool({}) False >>> bool({1: 1}) True >>> bool(None is not None) False >>> bool(1 is not None) True >>> bool(2 and 1 is not None) True 

In this code, you evaluate b is not None first, which is a boolean. 在此代码中,您首先评估b is not None ,这是一个布尔值。

And then a is implicitly converted into a boolean (For a list / dict , False if it is empty. For a number, False if it is 0) 然后a被隐式转换为布尔型(对于list / dictFalse如果它是空的。对于许多, False如果它是0)

then and is evaluated which always return a boolean. 然后and评价总是返回一个布尔值。

It is most certainly valid. 这当然是有效的。

When we take a variable which has been assigned a value and perform the AND operation with another variable with a null (in python - None), the AND operation results in a None . 当我们采取已经分配的值,并且与另一可变用空执行AND操作(在python -没有)的变量,该AND运算结果 Hence, the check can be made. 因此,可以进行检查。 To make my point clear, please refer below. 为了清楚说明,请参阅下文。

a=2;c=None; b=11
if a and c is not None:
    print("T")
else:
    print("F")

and

print(a and c)

results in None 结果为

and

print(a and b)

results in Non-null value - in our case 11 结果为非空值 - 在我们的例子中为11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM