这算不算 python 的小坑?

2016-03-02 15:30:44 +08:00
 haoc

看到这个两行代码,把我吓住了:
Python
if []:
print('False')

竟然没有报类型错误? Python 不是强类型么?
后来查了查 doc , if statement 在这里接受一个 expression 然后会调用 bool().
然后 bool([]) == False,所以没有出现类型错误。 -- 如果理解没错的话

感觉虽然这里没有出现[] == False 的问题但是这种 if [] 还是挺坑的,容易误导人。

3844 次点击
所在节点    Python
41 条回复
9hills
2016-03-02 15:34:47 +08:00
这个是 feature ,用处很多的,否则你判断字典和列表是不是空的,就复杂了
bobuick
2016-03-02 15:34:49 +08:00
if 0:
print 'i will not fuck'

=。=这就是 py 咯
gaoxt1983
2016-03-02 15:34:56 +08:00
这算啥坑啊,这个很方便的……
haoc
2016-03-02 15:38:27 +08:00
@9hills 难道这里不应该显示生命么?比如字典空和 null ref 怎么区分?比如下面这种情况?
a = None
b = []

print(bool(a))
print(bool(b))
Allianzcortex
2016-03-02 15:39:31 +08:00
这就是 Python ……说的真对

任何为空的 list,tuple 任何为 None 的对象,都是 False

类型一时爽……
haoc
2016-03-02 15:39:51 +08:00
@gaoxt1983 坑的感觉是这里让人以为它进行了 cast 。然后有种弱类型的感觉。感觉容易误导人。
est
2016-03-02 15:40:29 +08:00
ruby 没这个坑,结果代码里到处都是 .present? .empty? .blank? 还特么是 active support 才提供。
jarlyyn
2016-03-02 15:41:06 +08:00
pyhton 啥时候是强类型了?
chuan
2016-03-02 15:41:32 +08:00
这不是坑啊,正常的理解来说空字符串,空表都应该是 False 啊,很多人会 if my_list 这样写的。 lua 好像视 0 ,空字符为 True ,然而这才是例外啊
haoc
2016-03-02 15:42:57 +08:00
@bobuick 还是 py2 :)
haoc
2016-03-02 15:45:05 +08:00
@est 我不懂 ruby 。我是觉得这里的应该让用户显示的声明表达式。 bool()有点容易误导人。
Zzzzzzzzz
2016-03-02 15:46:15 +08:00
chuan
2016-03-02 15:47:02 +08:00
@haoc 显示调用 bool ?好像没什么语言这么做吧
haoc
2016-03-02 15:48:46 +08:00
zhuangzhuang1988
2016-03-02 15:50:15 +08:00
对的, 是坑...
haoc
2016-03-02 15:50:20 +08:00
@Zzzzzzzzz 可能我没表达清楚。不是显示调用 bool 。是声明表达式: len(arr) == 0 这样比较明确吧。
lxy
2016-03-02 15:52:28 +08:00
大家都比较懒嘛,如果不嫌麻烦的话可以
if a is not None and isinstance(a, list) and len(a) == 0:
print('empty list')
timonwong
2016-03-02 15:53:56 +08:00
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
haoc
2016-03-02 15:54:01 +08:00
@zhuangzhuang1988 第一次看别人这么写诧异了好久。
haoc
2016-03-02 15:55:04 +08:00
@chuan 回错了人。可能我没表达清楚。不是显示调用 bool 。是声明表达式: len(arr) == 0 这样比较明确吧。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/260519

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX