About MAKE_FUNCTION opcode in Python 3

A

Arnaud Delobelle

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 bar(x, y, *args, z=1, t=2, **kwargs): pass
....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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top