[Python-ideas] Python docs page: In what ways is None special
Michael Selik
mike at selik.org
Thu Jul 26 19:20:23 EDT 2018
On Mon, Jul 23, 2018 at 2:03 AM Jonathan Fine <jfine2358 at gmail.com> wrote:
> Thank you for your attention. What have I missed?
>
None and a few other things are special-cased by CPython. The compiler
won't bother to write bytecode instructions when an if-statement obviously
evaluates false. That might surprise some folks.
In [1]: import dis
In [2]: def foo():
...: if None:
...: print(1)
...: if 0:
...: print(2)
...: if 'a' == 'b':
...: print(3)
...:
In [3]: dis.dis(foo)
6 0 LOAD_CONST 1 ('a')
2 LOAD_CONST 2 ('b')
4 COMPARE_OP 2 (==)
6 POP_JUMP_IF_FALSE 16
7 8 LOAD_GLOBAL 0 (print)
10 LOAD_CONST 3 (3)
12 CALL_FUNCTION 1
14 POP_TOP
>> 16 LOAD_CONST 0 (None)
18 RETURN_VALUE
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180726/408faa93/attachment.html>
More information about the Python-ideas
mailing list