From: Paul Prescod Date: 2003-03-21T14:28:05+09:00 Subject: Re: Strong advantages over Python Joel VanderWerf wrote: > ... > icon = Icon.new(...) > > SliderControl.new("Horizontal position") do |new_x| > icon.move_to(new_x, icon.y) > end > > Could this be made so elegant in Python? It's almost identical in Python. icon = Icon(...) slider = SliderControl("Horizontal position", lambda new_x: icon.move_to(new_x, icon.y)) == The only limitation of Python's closures is _overwriting_ variables inherited from an intermediate scope. Note that there are several unusual things that need to happen to run into problems: 1. You need to define a function in a function. someglobal = someClass() def a(): def b(): .... return b But that's not enough. This code works. 2. The inner function must do an assignment to something outside of its scope. someglobal = someClass() def a(): def b(): someglobal.x = 5 return b But that's not enough. This code works. 3. The assignment must be to something in an intermediate scope. def a(): someinter = someClass() def b(): someinter.x = 5 return b Not enough. 4. The intermediate assignment must be to a variable, not a property of an object or a method call. Even though Python is an object oriented language, so most non-local assignments are to object properties. def a(): someinter = 0 def b(): someinter = 5 return b Now you'll run into the problem. And the workaround adds roughly 10 characters to the program. Paul Prescod