python-bool

Question

False不是False,而是True!?

def foolish(value):
    print 'Foolish!', value, type(value),
    if value:
        print "True"
    else:
        print "False"

if __name__ == "__main__":
    foolish('False')

打印结果:

Foolish! False  True

原因分析

虽然False不是True,但字符串’False’,的确是True。

bool()小函数

下面的bool()小函数用于避免这种低级错误,将字符串的’True’, ‘False’, 整数的0, 1转换成真正的Boolean type。

#!/usr/bin/env python

import types
import string
import sys

# Transform string or int value to boolean.
def bool(a):
    if type(a) == types.StringType:
        tmp = a.lower()
        if tmp == 'false':
            return False 
        if tmp == 'true':
            return True 
        raise ValueError, "Invalid bool value."

    if type(a) == types.IntType:
        if a == 0:
            return False
        if a == 1:
            return True
        raise ValueError, "Invalid bool value."

    if type(a) == types.BooleanType:
        return a 

    raise ValueError, "Invalid bool value."

def helper(value):
    print value, type(value),
    try:
        print bool(value)
    except Exception as e:
        print e 

def foolish(value):
    print 'Foolish!', value, type(value),
    if value:
        print "True"
    else:
        print "False"

if __name__ == "__main__":
    foolish('False')
    foolish('True')

    values = [None, True, False, 0, 1, 'True', 'false', 2, 't', (3, 1)]
    for value in values:
        helper(value)

    if len(sys.argv) == 2:
        helper(sys.argv[1])

运行结果

$ ./test.py True
Foolish! False  True
Foolish! True  True
None  Invalid bool value.
True  True
False  False
0  False
1  True
True  True
false  False
2  Invalid bool value.
t  Invalid bool value.
(3, 1)  Invalid bool value.
True  True

你可能感兴趣的:(Python)