我正在查看一些显然运行的代码,因为没有人抱怨它,但我对它们所写的内容感到困惑:
if a and b is not None:
# do something
Run Code Online (Sandbox Code Playgroud)
我一直认为'和'运算符是返回True或False的东西,现在我开始怀疑自己..它还会返回什么,一个数字......它可能不是pythonic,但我错过了什么 - 怎么能有人写这样的东西?
这意味着是否a is Truthy,b 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")
Run Code Online (Sandbox Code Playgroud)