Question about code writing '% i, callback'
fl
rxjwg98 at gmail.com
Mon Nov 30 12:50:42 EST 2015
On Monday, November 30, 2015 at 12:37:52 PM UTC-5, Terry Reedy wrote:
> On 11/30/2015 11:44 AM, fl wrote:
>
> > I come across the following code snippet.
>
> > for i in range(10):
> > def callback():
> > print "clicked button", i
> > UI.Button("button %s" % i, callback)
>
> > http://effbot.org/zone/default-values.htm
>
> Note that the above is an intentional example of common buggy code. It
> is followed by a version that works, with 'i=i' added to the callback
> header.
>
> --
> Terry Jan Reedy
With the following code, there is no bug as the original author said.
class buibutton():
print 'sd'
def __nonzero__(self):
return False
def Button(self, ii, callbackk):
callbackk()
return
for i in range(10):
def callback():
print "clicked button", i
UI.Button("button %s" % i, callback)
only to find that all callbacks print the same value (most likely 9, in this case).
Why does it have no bug?
More information about the Python-list
mailing list