华为手机网站建设策划方案,海南省住房公积金管理局咨询电话,响水做网站的价格,如何通过后台管理在网站的logo后台上加链接写习惯了C#的代码#xff0c;在想要将一个字符串False转换为bool型的时候#xff0c;很自然的写了如下的Python代码#xff1a; 看到上面的结果了没#xff1f;是True。突然记起Python中除了、、0、()、[]、{}、None为False之外#xff0c;其他的都是True。也…写习惯了C#的代码在想要将一个字符串False转换为bool型的时候很自然的写了如下的Python代码 看到上面的结果了没是True。突然记起Python中除了、、0、()、[]、{}、None为False之外其他的都是True。也就是说上面的False就是一个不为空的字符串所以结果就为True了。 为了深入了解下Python的bool类型就看了下说明 help(True)Help on bool object: class bool(int) | bool(x) - bool | | Returns True when the argument x is true, False otherwise. | The builtins True and False are the only two instances of the class bool. | The class bool is a subclass of the class int, and cannot be subclassed. | | Method resolution order: | bool | int | object | | Methods defined here: | | __and__(...) | x.__and__(y) xy | | __or__(...) | x.__or__(y) x|y 可以看到bool是int的子类来的并且不可以子类化 因为bool为int的子类所以用1表示True0表示False 看到上面2True是为false的。但是我们看下面的代码 我们看到True被打印出来了我想这样是因为if语句会在内部去调用bool()方法 因为bool是继承自int类型的所以我猜想在比较的时候最终是会转换为0和1来比较的就像 (注这里只是猜想未经证实) 既然bool是继承自int类型的所以很自然bool类型是支持数学运算的 最后我能想到的判断字符串是否有False的就是 不知道是否有更好的方法呢转载于:https://www.cnblogs.com/QLeelulu/archive/2009/09/20/1570292.html