hogeとはワイルドカードのようなものです。日々起こった、さまざまなこと −すなわちワイルドカード− を取り上げて日記を書く、という意味で名付けたのかというとそうでもありません。適当に決めたらこんな理由が浮かんできました。
01/24/2014 あ [長年日記]
tDiary 4110日目
■ [メモ][Py] a not in b と not a in b とド・モルガンの法則と
どうも not a in b が気に食わないので調べてみる. それぞれ test1,2,3 と test4,5,6 が組.
def test1(x):
return 0 not in x
def test2(x):
return not 0 in x
def test3(x):
return not (0 in x)
def test4(x):
return (0 not in x) and (1 not in x)
def test5(x):
return (not 0 in x) and (not 1 in x)
def test6(x):
return not ((0 in x) or (1 in x))
if __name__ == '__main__':
import dis
from operator import itemgetter
for name, obj in sorted(globals().items(), key=itemgetter(0)):
if not name.startswith('test'):
continue
print(name)
dis.dis(obj)
実行結果.
test1
2 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 7 (not in)
9 RETURN_VALUE
test2
5 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 7 (not in)
9 RETURN_VALUE
test3
8 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 7 (not in)
9 RETURN_VALUE
test4
11 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 7 (not in)
9 JUMP_IF_FALSE_OR_POP 21
12 LOAD_CONST 2 (1)
15 LOAD_FAST 0 (x)
18 COMPARE_OP 7 (not in)
>> 21 RETURN_VALUE
test5
14 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 7 (not in)
9 JUMP_IF_FALSE_OR_POP 21
12 LOAD_CONST 2 (1)
15 LOAD_FAST 0 (x)
18 COMPARE_OP 7 (not in)
>> 21 RETURN_VALUE
test6
17 0 LOAD_CONST 1 (0)
3 LOAD_FAST 0 (x)
6 COMPARE_OP 6 (in)
9 JUMP_IF_TRUE_OR_POP 21
12 LOAD_CONST 2 (1)
15 LOAD_FAST 0 (x)
18 COMPARE_OP 6 (in)
>> 21 UNARY_NOT
22 RETURN_VALUE
てっきり not a in b は「a in b のち not」とされるかと思ったら,そうでもなかった.
ただし組み合わせの場合においてド・モルガンの法則を適用し not を外に出すと,当然別の解釈となる. もちろん a is not b と not a is b についても同様なので,全国のド・モルガニストの諸兄は注意されたい.
ちなみに python 2.7.3 と 3.2.3 で違いはないっぽい.
[ツッコミを入れる]