On Fri, Jul 27, 2018 at 9:20 AM, Michael Selik <m...@selik.org> wrote:
> On Mon, Jul 23, 2018 at 2:03 AM Jonathan Fine <jfine2...@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
>

That's true of ANY constant, though. And a future version of Python
could well constant-fold the comparison, and thus optimize out the
third condition too. There's nothing special about None here.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to