
看到这个两行代码,把我吓住了:
Python
if []:
print('False')
竟然没有报类型错误? Python 不是强类型么?
后来查了查 doc , if statement 在这里接受一个 expression 然后会调用 bool().
然后 bool([]) == False,所以没有出现类型错误。 -- 如果理解没错的话
感觉虽然这里没有出现[] == False 的问题但是这种 if [] 还是挺坑的,容易误导人。
1 9hills Mar 2, 2016 这个是 feature ,用处很多的,否则你判断字典和列表是不是空的,就复杂了 |
2 bobuick Mar 2, 2016 if 0: print 'i will not fuck' =。=这就是 py 咯 |
3 gaoxt1983 Mar 2, 2016 这算啥坑啊,这个很方便的…… |
4 haoc OP |
5 Allianzcortex Mar 2, 2016 这就是 Python ……说的真对 任何为空的 list,tuple 任何为 None 的对象,都是 False 类型一时爽…… |
7 est Mar 2, 2016 ruby 没这个坑,结果代码里到处都是 .present? .empty? .blank? 还特么是 active support 才提供。 |
8 jarlyyn Mar 2, 2016 pyhton 啥时候是强类型了? |
9 chuan Mar 2, 2016 这不是坑啊,正常的理解来说空字符串,空表都应该是 False 啊,很多人会 if my_list 这样写的。 lua 好像视 0 ,空字符为 True ,然而这才是例外啊 |
12 Zzzzzzzzz Mar 2, 2016 |
14 haoc OP |
15 zhuangzhuang1988 Mar 2, 2016 对的, 是坑... |
17 lxy Mar 2, 2016 大家都比较懒嘛,如果不嫌麻烦的话可以 if a is not None and isinstance(a, list) and len(a) == 0: print('empty list') |
18 timonwong Mar 2, 2016 Python2 https://docs.python.org/2/reference/datamodel.html#object.__nonzero__ object.__nonzero__(self) Called to implement truth value testing and the built-in operation bool(); should return False or True, or their integer equivalents 0 or 1. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __nonzero__(), all its instances are considered true. Python3 https://docs.python.org/3.1/reference/datamodel.html#object.__bool__ object.__bool__(self) Called to implement truth value testing and the built-in operation bool(); should return False or True. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true. 如果要判断元素空,比如一个 list ,用 len(l) 如果要判断元素为 None, 用 is None |
19 haoc OP @zhuangzhuang1988 第一次看别人这么写诧异了好久。 |
21 zhuangzhuang1988 Mar 2, 2016 @haoc 对的 好多东西似乎写起来爽.项目大的时候真想骂娘.. |
22 lovepython Mar 2, 2016 这不是坑啊, 我记得基础时,就说明了, [ ] ( ) None 都是假 这是 python 特点,不让写那么多,简洁。哈哈 |
25 hahastudio Mar 2, 2016 嗯,我不懂 /不习惯 falsy value ,所以它是坑 |
26 strahe Mar 2, 2016 技巧和坑你确定分清楚了? |
27 mulog Mar 2, 2016 不算 只要记得有些地方需要对 None 和 empty list/dict 分开处理 这个特性还是很好用的 不能一个人看到某个写法觉得奇怪就是坑吧? 如果一门语言的一切写法你都觉得很合逻辑, 那很可能他和你已经会的一门语言基本差不多,那学来干什么。。 觉得还是起码先看完官方 doc 才来说坑不坑的问题吧 不看 doc 就说被坑了实在是。。 |
28 haoc OP @hahastudio 哈哈,好吧。我查到 python 有关于 truth value testing 的 doc 。是我小白了:) |
30 clino Mar 2, 2016 我觉得这是一个很好的 feature 我记得以前看过相关文档的,结果现在找不到说这个的文档了 |
31 jarlyyn Mar 2, 2016 |
32 displayabc Mar 2, 2016 @jarlyyn python 是强类型,动态类型 |
34 imlonghao Mar 2, 2016 我感觉这个挺好的... |
39 noahlee Mar 2, 2016 差点没反应过来, 就是个空列表, 挺方便的! |
40 happywowwow Mar 3, 2016 LZ 觉得 if 是用来判断 bool 值的 所以觉得 python 里面把 [] 计算成 bool 值是个坑 但是嘞 python 偷了很多懒 None () [] 什么的都是可以 if 的 |
41 waner55 Mar 3, 2016 https://www.python.org/dev/peps/pep-0008/#id45 看 sequese > For sequences, (strings, lists, tuples), use the fact that empty sequences are false. > ``` Yes: if not seq: if seq: No: if len(seq): if not len(seq): ``` |