About MAKE_FUNCTION opcode in Python 3

Arnaud Delobelle arnodel at gmail.com
Tue Sep 20 21:59:14 CEST 2011


Since Python 3.0 we have keyword only arguments in functions (see PEP
3102).  Looking at the documentation for the dis module (where opcodes
are documented), I see the following for MAKE_FUNCTION [1]

"""
MAKE_FUNCTION(argc)
Pushes a new function object on the stack. TOS is the code associated
with the function. The function object is defined to have argc default
parameters, which are found below TOS.
"""

No mention of default values for keyword only arguments.  Now let's
try (Python 3.2):

>>> def foo():
...   def bar(x, y, *args, z=1, t=2, **kwargs): pass
...
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 ('z')
              3 LOAD_CONST               2 (1)
              6 LOAD_CONST               3 ('t')
              9 LOAD_CONST               4 (2)
             12 LOAD_CONST               5 (<code object bar at
0x1005ec8b0, file "<stdin>", line 2>)
             15 MAKE_FUNCTION          512
             18 STORE_FAST               0 (bar)
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE

MAKE_FUNCTION has an argc of 512.  So it seems that since Python 3.0:

* the number of default values for normal arguments is argc & 0xFF
* the number of default values for keyword only arguments is argc >> 8

Can anyone confirm this?  I can then open a ticket on bugs.python.org

[1] http://docs.python.org/dev/library/dis.html#opcode-MAKE_FUNCTION

-- 
Arnaud



More information about the Python-list mailing list