@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2484 / 12833)
To distinguish None from a boolean False value, use Python's is operator:
実装してみた。
def is_none(src):
if src is None:
print('None')
elif src:
print('True')
else:
print('False')
is_none(None)
is_none(0)
is_none([])
is_none({})
is_none(True)
print
is_none(1)
is_none([3.14, 2.718])
is_none({'pi', 3.14})
Success time: 0.01 memory: 23304 signal:0
None
False
False
False
True
True
True
True
http://monmon.hateblo.jp/entry/20110214/1297710749
上記のリンクに記載の以下も参考になる。
http://jaredgrubb.blogspot.jp/2009/04/python-is-none-vs-none.html
https://docs.python.org/3.6/library/operator.html#operator.is_
operator.is_(a, b)
Return a is b. Tests object identity.
https://docs.python.org/3.6/library/operator.html#mapping-operators-to-functions
Syntaxがa is b
で、Functionがis_(a, b)
@shiracamus さんの[コメント](http://qiita.com/7of9/items/51ed254530dffb5a0815/#comment-e687d65ea5710da8821b)にて以下の事項を教えていただきました。 情報感謝です。
- isはオブジェクトが同一か比較
- オブジェクトid
- id(a)
-
==
は同値か比較 - Flyweightパターン
- 関連: http://qiita.com/u_kan/items/bdef41767fc526e1da0f
- 関連: http://docs.python.jp/3/c-api/long.html
-
現在の実装では、-5 から 256 までの全ての整数に対する整数オブジェクトの配列を保持するようにしており、...
- 数値比較には is ではなく == を使いましょう