On Wed, Aug 27, 2008 at 2:27 PM, Nick Coghlan <[EMAIL PROTECTED]> wrote:
   ...
> Late binding default arguments would do some fairly bad things to nested
> functions such as preventing the use of class attributes when defining

They'd also be SERIOUSLY problematic wrt a VERY common issue -- one
that I had an excellent and Python-experienced colleague ask me about
just the other day as we were waiting for a meeting room to actually
get free for a meeting we were both at -- rephrasing and summarizing,
his problem was like:

for text, command in zip(labels, commands):
  makebutton(text, lambda evt: command)

and the LAST command was being bound to all buttons (of course).  With
current semantics I was able to give him the solution instantly (while
others were still streaming out of the conference room;-):

for text, command in zip(labels, commands):
  makebutton(text, lambda evt, command=command: command)

If default args were late-bound, I'd have to offer exclusively the
more-complex "closure" approach:

def makecommand(command):
    def doit(evt): return command()
    return doit
for text, command in zip(labels, commands):
  makebutton(text, makecommand(command))

which -- while I personally prefer it -- is _definitely_
heavier-weight.  This issue does happen A LOT: I even saw it
highlighted in a popular text on Javascript (so, it's not even a
Python-only one;-).


Alex
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to