bool("")

>>> download_complete = False
>>> bool(download_complete)
False
>>> bool(-1.23)
True
>>> bool(0.0)
False
>>> bool("")
False
>>> bool([None, 0])
True
>>> bool([0, 0])
True
>>> bool({0, 0, 0})
True
>>> bool 0
SyntaxError: invalid syntax
>>> boo(0)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    boo(0)
NameError: name 'boo' is not defined
>>> bool(0)
False
>>> bool("")
False
>>>

in python: the "" and 0 will be treat as false, other variables will be treated as the True.

And the

if a:

and

if bool(a) :

has the same result in the test.  other examples is that if there is none member in the list the bool(liat_example) will be false.

原文地址:https://www.cnblogs.com/henyihanwobushi/p/2662428.html