2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで色々関数をdisして眺める

Last updated at Posted at 2017-12-11

概要

Pythonで色々にdisして眺める。

環境

os: maxOS Sierra Version 10.12.6
python: Python 3.6.0

内容

適当に関数fを定義し、dis.disを用いて内部構造をみた。
(予め、import disを行なっておいた。)
(参考URL: https://docs.python.jp/3/library/dis.html)

インタープリタ
>>> def f(a, b):
...     pass
...
>>> dis.dis(f)
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     return 1
...
>>> dis.dis(f)
  2           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     return a
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     a
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 POP_TOP
              4 LOAD_CONST               0 (None)
              6 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     a + b
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 BINARY_ADD
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     a - b
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 BINARY_SUBTRACT
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     print('hello')
...
>>> dis.dis(f)
  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('hello')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     c = 'hello'
...     print(c)
...
>>> dis.dis(f)
  2           0 LOAD_CONST               1 ('hello')
              2 STORE_FAST               2 (c)

  3           4 LOAD_GLOBAL              0 (print)
              6 LOAD_FAST                2 (c)
              8 CALL_FUNCTION            1
             10 POP_TOP
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     for i in range(10):
...         pass
...
>>> dis.dis(f)
  2           0 SETUP_LOOP              16 (to 18)
              2 LOAD_GLOBAL              0 (range)
              4 LOAD_CONST               1 (10)
              6 CALL_FUNCTION            1
              8 GET_ITER
        >>   10 FOR_ITER                 4 (to 16)
             12 STORE_FAST               2 (i)

  3          14 JUMP_ABSOLUTE           10
        >>   16 POP_BLOCK
        >>   18 LOAD_CONST               0 (None)
             20 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     for i in range(10):
...         print(i)
...
>>> dis.dis(f)
  2           0 SETUP_LOOP              24 (to 26)
              2 LOAD_GLOBAL              0 (range)
              4 LOAD_CONST               1 (10)
              6 CALL_FUNCTION            1
              8 GET_ITER
        >>   10 FOR_ITER                12 (to 24)
             12 STORE_FAST               2 (i)

  3          14 LOAD_GLOBAL              1 (print)
             16 LOAD_FAST                2 (i)
             18 CALL_FUNCTION            1
             20 POP_TOP
             22 JUMP_ABSOLUTE           10
        >>   24 POP_BLOCK
        >>   26 LOAD_CONST               0 (None)
             28 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     [1 for i in range(10)]
...
>>> dis.dis(f)
  2           0 LOAD_CONST               1 (<code object <listcomp> at 0x101a04d20, file "<stdin>", line 2>)
              2 LOAD_CONST               2 ('f.<locals>.<listcomp>')
              4 MAKE_FUNCTION            0
              6 LOAD_GLOBAL              0 (range)
              8 LOAD_CONST               3 (10)
             10 CALL_FUNCTION            1
             12 GET_ITER
             14 CALL_FUNCTION            1
             16 POP_TOP
             18 LOAD_CONST               0 (None)
             20 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     if 1:
...         pass
...
>>> dis.dis(f)
  3           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     if a:
...         pass
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 POP_JUMP_IF_FALSE        4

  3     >>    4 LOAD_CONST               0 (None)
              6 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     if a:
...         pass
...     else:
...         pass
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (a)
              2 POP_JUMP_IF_FALSE        6

  3           4 JUMP_FORWARD             0 (to 6)

  5     >>    6 LOAD_CONST               0 (None)
              8 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     a = (1, 2)
...     b = a[0]
...
>>> dis.dis(f)
  2           0 LOAD_CONST               4 ((1, 2))
              2 STORE_FAST               0 (a)

  3           4 LOAD_FAST                0 (a)
              6 LOAD_CONST               3 (0)
              8 BINARY_SUBSCR
             10 STORE_FAST               1 (b)
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE
インタープリタ
>>> def f(a, b):
...     a = {'hello': 'world'}
...     b = a['hello']
...
>>> dis.dis(f)
  2           0 LOAD_CONST               1 ('hello')
              2 LOAD_CONST               2 ('world')
              4 BUILD_MAP                1
              6 STORE_FAST               0 (a)

  3           8 LOAD_FAST                0 (a)
             10 LOAD_CONST               1 ('hello')
             12 BINARY_SUBSCR
             14 STORE_FAST               1 (b)
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE

感想

楽しい。
(参考:https://qiita.com/tans/items/946ef3a69a23c50618f9

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?