[Python-3000] optional argument annotations
Tony Lownds
tony at PageDNA.com
Thu Nov 23 19:00:01 CET 2006
I have a working optional argument syntax implementation, I'm hoping
to get some direction on
the implementation decisions so far.... please see below.
Python 3.0x (p3yk:52824M, Nov 23 2006, 09:22:23)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f()-> 1: pass
...
>>> f.func_returns
1
>>> def f(): pass
...
>>> f.func_annotations
>>> f.func_returns
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'func_returns'
>>> def f(x:1): pass
...
>>> f.func_annotations
{'x': 1}
>>>
>>> import dis
>>> dis.dis(compile("def f(x:1, y:2=3, z=4)->5:pass", "<str>", "exec"))
1 0 LOAD_CONST 0 (3) # default for y
3 LOAD_CONST 1 (4) # default for z
6 LOAD_CONST 2 (1) # annotation
for x
9 LOAD_CONST 3 (2) # annotation
for y
12 LOAD_CONST 4 (6) # bitmask for
annotations... 0b110
15 LOAD_CONST 5 (5) # func_returns
18 LOAD_CONST 6 (<code object f at
0xb7f08848, file "<str>", line 1>)
21 MAKE_FUNCTION 49154 # numdefaults(2) +
kwdefaults(0) << 8 + has_annotations(1) << 14 + has_returns(1) << 15
24 STORE_NAME 0 (f)
27 LOAD_CONST 7 (None)
30 RETURN_VALUE
Index: Parser/Python.asdl
===================================================================
--- Parser/Python.asdl (revision 52824)
+++ Parser/Python.asdl (working copy)
@@ -9,8 +9,8 @@
-- not really an actual node but useful in Jython's
typesystem.
| Suite(stmt* body)
- stmt = FunctionDef(identifier name, arguments args,
- stmt* body, expr* decorators)
+ stmt = FunctionDef(identifier name, typedarguments args,
+ stmt* body, expr* decorators, expr? returns)
| ClassDef(identifier name, expr* bases, stmt* body)
| Return(expr? value)
@@ -102,6 +102,9 @@
arguments = (expr* args, identifier? vararg, expr* kwonlyargs,
identifier? kwarg, expr* defaults, expr*
kw_defaults)
+ typedarguments = (annotatedarg* args, identifier? vararg,
expr* kwonlyargs,
+ identifier? kwarg, expr* defaults, expr*
kw_defaults)
+ annotatedarg = (expr arg, expr? annotation)
-- keyword arguments supplied to call
keyword = (identifier arg, expr value)
More information about the Python-3000
mailing list