看到这个两行代码,把我吓住了:
Python
if []:
print('False')
竟然没有报类型错误? Python 不是强类型么?
后来查了查 doc , if statement 在这里接受一个 expression 然后会调用 bool().
然后 bool([]) == False,所以没有出现类型错误。 -- 如果理解没错的话
感觉虽然这里没有出现[] == False 的问题但是这种 if [] 还是挺坑的,容易误导人。
看到这个两行代码,把我吓住了:
Python
if []:
print('False')
竟然没有报类型错误? Python 不是强类型么?
后来查了查 doc , if statement 在这里接受一个 expression 然后会调用 bool().
然后 bool([]) == False,所以没有出现类型错误。 -- 如果理解没错的话
感觉虽然这里没有出现[] == False 的问题但是这种 if [] 还是挺坑的,容易误导人。
|
9hillsMar 2, 2016
1
这个是 feature ,用处很多的,否则你判断字典和列表是不是空的,就复杂了
|
|
bobuickMar 2, 2016
2
if 0:
print 'i will not fuck' =。=这就是 py 咯 |
|
gaoxt1983Mar 2, 2016
3
这算啥坑啊,这个很方便的……
|
|
4
|
|
AllianzcortexMar 2, 2016
5
这就是 Python ……说的真对
任何为空的 list,tuple 任何为 None 的对象,都是 False 类型一时爽…… |
|
estMar 2, 2016
7
ruby 没这个坑,结果代码里到处都是 .present? .empty? .blank? 还特么是 active support 才提供。
|
|
jarlyynMar 2, 2016
8
pyhton 啥时候是强类型了?
|
|
chuanMar 2, 2016
9
这不是坑啊,正常的理解来说空字符串,空表都应该是 False 啊,很多人会 if my_list 这样写的。 lua 好像视 0 ,空字符为 True ,然而这才是例外啊
|
|
ZzzzzzzzzMar 2, 2016
12
|
|
14
|
|
zhuangzhuang1988Mar 2, 2016
15
对的, 是坑...
|
|
lxyMar 2, 2016
17
大家都比较懒嘛,如果不嫌麻烦的话可以
if a is not None and isinstance(a, list) and len(a) == 0: print('empty list') |
|
timonwongMar 2, 2016
18
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
@zhuangzhuang1988 第一次看别人这么写诧异了好久。
|
|
zhuangzhuang1988Mar 2, 2016
21
@haoc 对的 好多东西似乎写起来爽.项目大的时候真想骂娘..
|
|
lovepythonMar 2, 2016
22
这不是坑啊,
我记得基础时,就说明了, [ ] ( ) None 都是假 这是 python 特点,不让写那么多,简洁。哈哈 |
|
25
嗯,我不懂 /不习惯 falsy value ,所以它是坑
|
|
straheMar 2, 2016
26
技巧和坑你确定分清楚了?
|
|
mulogMar 2, 2016
27
不算 只要记得有些地方需要对 None 和 empty list/dict 分开处理 这个特性还是很好用的
不能一个人看到某个写法觉得奇怪就是坑吧? 如果一门语言的一切写法你都觉得很合逻辑, 那很可能他和你已经会的一门语言基本差不多,那学来干什么。。 觉得还是起码先看完官方 doc 才来说坑不坑的问题吧 不看 doc 就说被坑了实在是。。 |
|
28
@hahastudio 哈哈,好吧。我查到 python 有关于 truth value testing 的 doc 。是我小白了:)
|
|
clinoMar 2, 2016
30
我觉得这是一个很好的 feature
我记得以前看过相关文档的,结果现在找不到说这个的文档了 |
|
jarlyynMar 2, 2016
31
|
|
displayabcMar 2, 2016
32
@jarlyyn python 是强类型,动态类型
|
|
imlonghaoMar 2, 2016
34
我感觉这个挺好的...
|
|
noahleeMar 2, 2016
39
差点没反应过来, 就是个空列表, 挺方便的!
|
|
happywowwowMar 3, 2016
40
LZ 觉得 if 是用来判断 bool 值的
所以觉得 python 里面把 [] 计算成 bool 值是个坑 但是嘞 python 偷了很多懒 None () [] 什么的都是可以 if 的 |
|
waner55Mar 3, 2016
41
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): ``` |