On 9/20/2011 5:56 PM, Eric Snow wrote:
On Tue, Sep 20, 2011 at 1:59 PM, Arnaud Delobelle<arno...@gmail.com> wrote:
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
You're mostly right.
http://hg.python.org/cpython/file/default/Python/ceval.c#l2684
2684 int posdefaults = oparg& 0xff;
2685 int kwdefaults = (oparg>>8)& 0xff;
2686 int num_annotations = (oparg>> 16)& 0x7fff;
[1] http://docs.python.org/dev/library/dis.html#opcode-MAKE_FUNCTION
I agree that doc should be updated for 3.x. Please suggest a new working
if you can, as well as copying the source snippet above. Add me
terry.reedy as nosy.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list