Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99756
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Question about code writing '% i, callback' |
| Date | 2015-11-30 11:24 -0700 |
| Message-ID | <mailman.48.1448907905.14615.python-list@python.org> (permalink) |
| References | <25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com> <f18de0eb-07f1-424a-90c3-62fbc9be128a@googlegroups.com> |
On Mon, Nov 30, 2015 at 10:36 AM, fl <rxjwg98@gmail.com> wrote:
> Thanks for the replies. Now, I have the following code:
>
>
>
> class buibutton():
> print 'sd'
> def __nonzero__(self):
> return False
>
> def Button(self, ii, callbackk):
> callbackk()
> return
> UI=buibutton()
>
>
> for i in range(10):
> def callback():
> print "clicked button", i
> UI.Button("button %s" % i, callback)
>
>
> To my surprise, the output is not the original link expected. i.e. it is
> the same with binding to the current values:
The callback function is being called immediately, in the body of the
loop, not stored and called later. The value of i in the closure has
not actually changed yet at the point you're calling it. If you
instead store the callback and call it later, you'll find that each
message says "button 9".
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question about code writing '% i, callback' fl <rxjwg98@gmail.com> - 2015-11-30 08:44 -0800
Re: Question about code writing '% i, callback' Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-11-30 10:53 -0600
Re: Question about code writing '% i, callback' Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-11-30 10:54 -0600
Re: Question about code writing '% i, callback' Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-30 11:01 -0600
Re: Question about code writing '% i, callback' fl <rxjwg98@gmail.com> - 2015-11-30 10:55 -0800
Re: Question about code writing '% i, callback' Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-30 20:44 -0500
Re: Question about code writing '% i, callback' Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-01 09:53 -0600
Re: Question about code writing '% i, callback' fl <rxjwg98@gmail.com> - 2015-11-30 09:36 -0800
Re: Question about code writing '% i, callback' Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-30 11:24 -0700
Re: Question about code writing '% i, callback' Terry Reedy <tjreedy@udel.edu> - 2015-11-30 12:36 -0500
Re: Question about code writing '% i, callback' fl <rxjwg98@gmail.com> - 2015-11-30 09:50 -0800
csiph-web