PEP 312 (and thus 308) implemented with a black magic trick

Anders J. Munch andersjm at dancontrol.dk
Thu Mar 20 04:53:53 EST 2003


"Terry Reedy" <tjreedy at udel.edu> wrote:
> 
> 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
> ...
[...]
> 
> Compiler seems to optimize away second unary positive.
> 

The second unary positive is treated as part of the integer literal.
Try instead disassembling

 def f(x): ++x

You can have as many UNARY_POSITIVEs as you like.

- Anders







More information about the Python-list mailing list