closures and dynamic binding

Steven D'Aprano steven at
Mon Sep 29 01:06:58 CEST 2008

On Sun, 28 Sep 2008 17:47:44 -0400, Terry Reedy wrote:

> Aaron "Castironpi" Brady wrote:
>> On Sep 28, 2:52 am, Steven D'Aprano <st... at REMOVE-THIS-
>>> As for why the complicated version works, it may be clearer if you
>>> expand it from a one-liner:
>>> # expand: f[ n ]= (lambda n: ( lambda: n ) )( n )
>>> inner = lambda: n
>>> outer = lambda n: inner
>>> f[n] = outer(n)
>>> outer(0) => inner with a local scope of n=0 outer(1) => inner with a
>>> local scope of n=1 etc.
> For this to work, the 'expansion' has to be mental and not actual. Which
> is to say, inner must be a text macro to be substituted back into outer.

Er, yes, that's what I meant, sorry for not being more explicit. That's 
why it wasn't a copy and paste of actual running code.

Or perhaps I just confused myself and was talking nonsense.


More information about the Python-list mailing list