variable binding [was lambda decorators]

The proposal to add bind to the function definition is silly, since we can do the equivalent of def f(bind i, …) already using decorators:
Just put the bind decorator into functools and the problem is solved. This is better than the (UGLY!) default values hack, since in this case it is impossible for your caller to accidentally overwrite the value you wanted bound (or at least not without some stackframe manipulation, at which point you get what you deserve). I also don't like Guido's proposed var and new keywords. With all due respect, it looks like JavaScript. Besides, we all ready have a perfectly good tool for adding new scopes: functions. Just use a decorator like this map_maker to make an imap of the for-loop you wanted to have a separate scope in.
So, my proposal is that the API of the bind decorator be cleaned up considerably then added to the functools. The map_maker API seems to be good enough and could also go into functools. -- Carl

On Mon, Feb 9, 2009 at 4:46 PM, Carl Johnson <carl@carlsensei.com> wrote:
Sure, if you want to manufacture a whole new object each time through the loop and access the bound variable by digging deep into an extra argument, this might be considered less ugly. Personally if I had to go to such lengths I would prefer to just switch to an OO style of coding and make the function a genuine method (effectively you're making it a method anyway by implicitly passing self). As others implied, I don't see why you should go to such lengths to avoid having to write OO code. -1 on adding this UGLY hack to functools.
I guess UGLY is subjective. :-)
Such games with decorators look UGLY to me. How I am, poor reader, supposed to guess what the signature of my_map() is from reading its definition?
-1 again. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

Of course a much simpler decorator is possible: for i in range(10): @bind(i=i) def my_func(i): print i # Implementing bind is left as an exercise for the reader i is passed to bind as a keyword argument, which it saves and later passes to my_func as a keyword argument. No default values are used so it's no longer considered "ugly". It will also give an error if somebody accidentally passes in i themselves. I stand by my previous comment on it not being worthwhile. -- Adam Olsen, aka Rhamphoryncus

On Mon, Feb 9, 2009 at 4:46 PM, Carl Johnson <carl@carlsensei.com> wrote:
Sure, if you want to manufacture a whole new object each time through the loop and access the bound variable by digging deep into an extra argument, this might be considered less ugly. Personally if I had to go to such lengths I would prefer to just switch to an OO style of coding and make the function a genuine method (effectively you're making it a method anyway by implicitly passing self). As others implied, I don't see why you should go to such lengths to avoid having to write OO code. -1 on adding this UGLY hack to functools.
I guess UGLY is subjective. :-)
Such games with decorators look UGLY to me. How I am, poor reader, supposed to guess what the signature of my_map() is from reading its definition?
-1 again. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

Of course a much simpler decorator is possible: for i in range(10): @bind(i=i) def my_func(i): print i # Implementing bind is left as an exercise for the reader i is passed to bind as a keyword argument, which it saves and later passes to my_func as a keyword argument. No default values are used so it's no longer considered "ugly". It will also give an error if somebody accidentally passes in i themselves. I stand by my previous comment on it not being worthwhile. -- Adam Olsen, aka Rhamphoryncus
participants (3)
-
Adam Olsen
-
Carl Johnson
-
Guido van Rossum