[Python-ideas] Before and after the colon in funciton defs.

Nick Coghlan ncoghlan at gmail.com
Fri Sep 23 05:26:35 CEST 2011

On Fri, Sep 23, 2011 at 11:39 AM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
>> Decorator syntax cannot work without deep magic, because the
>> compiler *doesn't know* that injected names need to be given special
>> treatment.
> This is the last time you mention the decorator solution (aside from
> further explanation of the problem). Is it being discarded for that
> reason?

Yes, it's far too hard to explain what it does in terms of existing
Python semantics (the Zen has something to say on that point).
Bytecode hackery and other tricks would *work*, but deep magic should
only be employed when there aren't any alternatives and the problem is
sufficiently common. (PEP 3135, the new super semantics for 3.x,
pushes the boundaries of what's reasonable, but it's so easy to *use*
that it's worth the additional under the hood complexity). Magical
behaviour is also far more likely to cause problems for other
implementations - it tends to rely on assumptions that aren't
explicitly guaranteed by the language specification.

Use of default arguments for pre-initialised function locals is
nowhere near common enough to justify deep magic as a solution, so the
idea just isn't worth pursuing.

That's the real benefit of the "nonlocal i=i" idea: it takes an
*existing* concept (i.e. closures), and just tweaks it a bit by
avoiding the need for a separate outer scope when all you really want
to do is share some state between invocations. Anyone that already
understands closures shouldn't have much trouble grasping the idea
that the 'innermost containing scope' can now be the function itself
rather than the next level up. Any implementation that already
correctly handles closures should also be able to cope with the self
reference without too much trouble.

No new keywords, no new namespace semantics, just a slight tweak to
the way the compiler handles def and nonlocal statements.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list