PEP 312 (and thus 308) implemented with a black magic trick
Terry Reedy
tjreedy at udel.edu
Wed Mar 19 12:21:24 EST 2003
"Andrew Bennetts" <andrew-pythonlist at puzzling.org> wrote in message
news:mailman.1048071128.22193.python-list at python.org...
> On Mon, Mar 17, 2003 at 01:58:28PM -0800, Michele Simionato wrote:
> > Today I had an illumination on how to implement both PEP 312 and
308
> > in current Python. I think I will share the idea, even if I would
*not*
> > recommend to use such a dirty trick in production code.
>
> Nasty. :)
>
> So, who's going to be the first to implement ++x using this
technique?
If, as I understand, this meta technique relies on source text, this
should work. But not if it looks at code:
>>> def f(): return ++1
...
>>> import dis
>>> dis.dis(f)
0 SET_LINENO 1
3 SET_LINENO 1
6 LOAD_CONST 1 (1)
9 UNARY_POSITIVE
10 RETURN_VALUE
11 LOAD_CONST 0 (None)
14 RETURN_VALUE
Compiler seems to optimize away second unary positive.
Terry J. Reedy
More information about the Python-list
mailing list