I wonder what is better to do:
d = { 'a': 1, 'b': 2}'a' in dTrue
or:
d = { 'a': 1, 'b': 2}d.has_key('a')True
本文共 3192 字,大约阅读时间需要 10 分钟。
393 51 | I wonder what is better to do: d = { 'a': 1, 'b': 2}'a' in dTrue or: d = { 'a': 1, 'b': 2}d.has_key('a')True | |||
|
546 | In fact . | |||
|
179 | $ python -mtimeit -s'd=dict.fromkeys(range(99))' '12 in d'10000000 loops, best of 3: 0.0983 usec per loop$ python -mtimeit -s'd=dict.fromkeys(range(99))' 'd.has_key(12)'1000000 loops, best of 3: 0.21 usec per loop While the following observation is not always true, you'll notice that usually, in Python, the faster solution is more elegant and Pythonic; that's why | ||||||||||||
|
43 | According to python :
| ||||
|
28 | Use | ||||||||||||
|
13 | There is one example where If you use Fix is obviously trivial: def __contains__(self, x): return self.has_key(x) | ||
|
8 | |
转载地址:http://fvhbi.baihongyu.com/
in
works with 2.6 too right? –