Skip to content

Instantly share code, notes, and snippets.

@dcrosta
Created May 8, 2015 11:23

Revisions

  1. dcrosta created this gist May 8, 2015.
    34 changes: 34 additions & 0 deletions bare_if.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    >>> def foo(a, b, c):
    ... if a:
    ... return b
    ... return c
    ...
    >>> dis.dis(foo)
    2 0 LOAD_FAST 0 (a)
    3 POP_JUMP_IF_FALSE 10

    3 6 LOAD_FAST 1 (b)
    9 RETURN_VALUE

    4 >> 10 LOAD_FAST 2 (c)
    13 RETURN_VALUE


    >>> def foo(a, b, c):
    ... if a:
    ... return b
    ... else:
    ... return c
    ...
    >>> dis.dis(foo)
    2 0 LOAD_FAST 0 (a)
    3 POP_JUMP_IF_FALSE 10

    3 6 LOAD_FAST 1 (b)
    9 RETURN_VALUE

    5 >> 10 LOAD_FAST 2 (c)
    13 RETURN_VALUE
    14 LOAD_CONST 0 (None) # Note that control will never reach here
    17 RETURN_VALUE # so even though there's more bytecode, it
    # won't impact run-time performance