[Python-ideas] Tweaking closures and lexical scoping to include the function being defined
Nick Coghlan
ncoghlan at gmail.com
Sun Oct 2 14:48:33 CEST 2011
On Sun, Oct 2, 2011 at 4:16 AM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> surely the FUNC above ...
>
> > """DOC"""
> > nonlocal NAME
> > BODY
> > return FUNC
>
> ... doesn't need to be the *same identifier* as the FUNC below?
>
> > FUNC = <anon1>()
>
> Isn't magic needed solely to inject the nonlocal statement(s) into the
> definition of FUNC inside <anon1> at compile-time?
Well, having 'FUNC' the same from the compiler's point of view is also
necessary to get introspection to work properly (i.e. FUNC.__name__ ==
'FUNC').
But yeah, the fundamental challenge lies in telling the compiler to
change the way it binds and references certain names (like global and
nonlocal declarations) while also being able to initialise them at
function definition time (like default arguments).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list