0
爲什麼在Python中可以使用布爾值作爲索引?例如在Python中使用布爾值作爲數組索引
>>> a = [1, 2, 3, 4, 5]
>>> a[True]
2
>>> a[False]
1
由於python是一種強類型語言,編譯器不應該像在添加字符串和整數時一樣拋出TypeError嗎?例如
>>> "1" + 1
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: cant convert 'int' object to 'str' implicitly
>>> 1 + "1"
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
布爾子整數,你可以從'isinstance(真,INT)看'。 – jonrsharpe
http://stackoverflow.com/questions/2764017/is-false-0-and-true-1-in-python-an-implementation-detail-or-is-it-guarante –