python中的一个string的真值
if <boolean> : # do this 布尔值必须是True或False。
那么为什么
 if "poi": print "yes" 
输出:是的
我没有得到为什么是印刷,因为“poi”是真实的或错误的。
当expression式需要布尔值时,Python将尽最大努力评估expression式的“真实性”。
 string的规则是一个空string被认为是False ,一个非空string被认为是True 。 对其他容器施加相同的规则,因此空字典或列表被视为False ,包含一个或多个条目的字典或列表被视为True 。 
  None对象也被认为是false。 
 数值0被认为是错误的(尽pipestring值'0'被认为是真的)。 
 所有其他expression式被认为是True 。 
详细信息(包括用户定义types如何指定真实性)可以在这里find: http : //docs.python.org/release/2.5.2/lib/truth.html 。
 在python中,除空string以外的任何string默认为True 
即
 if "MyString": # this will print foo print("foo") if "": # this will NOT print foo print("foo") 
 这里发生的事情是Python之后隐式的bool()构造函数的补充,因为if后面的任何东西都应该被parsing为boolean。 在这种情况下,你的代码相当于 
 if bool("poi"): print "yes" 
 根据Python bool(x)构造函数根据以下情况接受任何东西并确定真实性 
-  如果x是整数,则只有0是False所有其他的都是True
-  如果x是float,则只有0.0是False其他的都是True
-  如果x是列表,则只有[]是False其他的都是True
-  如果x设置为/ dict,则只有{}是False其他的都是True
-  如果x是元组,则Only ()是False其他的都是True
-  如果x是string,那么只有“"是False其他的都是True。注意,bool(“False”)会返回True
这是我上面列出的案例的日志
 Python 3.4.3 (default, Feb 25 2015, 21:28:45) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> bool(0) False >>> bool(1) True >>> bool(-1) True >>> bool(0.0) False >>> bool(0.02) True >>> bool(-0.10) True >>> bool([]) False >>> bool([1,2]) True >>> bool(()) False >>> bool(("Hello","World")) True >>> bool({}) False >>> bool({1,2,3}) True >>> bool({1:"One", 2:"Two"}) True >>> bool("") False >>> bool("Hello") True >>> bool("False") True