lambda closure question

Duncan Booth duncan.booth at invalid.invalid
Mon Feb 21 08:13:04 EST 2005


Antoon Pardon wrote:

> But I'll get back at what seems you actually wanted to say:
> That there is no way to rebind 'x' or in my case 'l' and
> with that I have to agree although I personnaly find that
> a lack in python

'no way' is a bit strong. You can use hacks such as the one I posted a 
couple of weeks back:

>>> import hack
>>> def F():
	l = 'hello there'
	def get():
		return l
	return hack.mksetter(lambda: l), get

>>> set, get = F()
>>> get()
'hello there'
>>> set('testing')
'testing'
>>> get()
'testing'

'no official, supported, or portable way' would be more accurate.

I've never actually felt the need to set variables from inside a nested 
function, and I wouldn't use a hack like this in production code unless 
there was a *very* good reason for it, but since I figured out how to 
actually do it I'm suprised just how many things could actually be 
simplified if there was an official way to do this.



More information about the Python-list mailing list