Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99741 > unrolled thread

Question about code writing '% i, callback'

Started byfl <rxjwg98@gmail.com>
First post2015-11-30 08:44 -0800
Last post2015-11-30 09:50 -0800
Articles 11 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#99741 — Question about code writing '% i, callback'

Fromfl <rxjwg98@gmail.com>
Date2015-11-30 08:44 -0800
SubjectQuestion about code writing '% i, callback'
Message-ID<25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com>
Hi,

I come across the following code snippet.





for i in range(10):
    def callback():
        print "clicked button", i
    UI.Button("button %s" % i, callback)




The content inside parenthesis in last line is strange to me. 

"button %s" % i, callback


That is, the writing looks like recognized as three items when I try with a
class definition (it can run with this):

class buibutton():
    print 'sd'
    def __nonzero__(self):
       return False
       
    def Button(str, ii, callbackk):
        
        return


Could you explain it to me?

The link is here:

http://effbot.org/zone/default-values.htm

Thanks,

[toc] | [next] | [standalone]


#99743

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-11-30 10:53 -0600
Message-ID<mailman.39.1448902411.14615.python-list@python.org>
In reply to#99741
On Mon, Nov 30, 2015 at 10:44 AM, fl <rxjwg98@gmail.com> wrote:
> The content inside parenthesis in last line is strange to me.
>
> "button %s" % i, callback

https://docs.python.org/library/stdtypes.html#printf-style-string-formatting

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#99744

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-11-30 10:54 -0600
Message-ID<mailman.40.1448902515.14615.python-list@python.org>
In reply to#99741
On Mon, Nov 30, 2015 at 10:53 AM, Zachary Ware
<zachary.ware+pylist@gmail.com> wrote:
> On Mon, Nov 30, 2015 at 10:44 AM, fl <rxjwg98@gmail.com> wrote:
>> The content inside parenthesis in last line is strange to me.
>>
>> "button %s" % i, callback
>
> https://docs.python.org/library/stdtypes.html#printf-style-string-formatting

Sorry, should have tested that link before sending.  That should be either

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

or

https://docs.python.org/2/library/stdtypes.html#string-formatting-operations

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#99745

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-30 11:01 -0600
Message-ID<mailman.41.1448902962.14615.python-list@python.org>
In reply to#99741
On Mon, Nov 30, 2015 at 10:44 AM, fl <rxjwg98@gmail.com> 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)
>
> The content inside parenthesis in last line is strange to me.
>
> "button %s" % i, callback

These are the arguments being passed to UI.Button. The first argument is:

    "button %s" % i

This is an example of printf-style string formatting. See the link
that Zachary posted.

The second argument is the function named callback.

> That is, the writing looks like recognized as three items when I try with a
> class definition (it can run with this):
>
> class buibutton():
>     print 'sd'
>     def __nonzero__(self):
>        return False
>
>     def Button(str, ii, callbackk):
>
>         return
>
>
> Could you explain it to me?

How is this related to the example above?

Here, Button is defined as a method of a class. Since it's a method,
the first parameter is the "self" parameter, which will implicitly
take the value of the class instance that you're calling the Button
method on. If you're trying to call this like above, then the second
parameter "ii" will take the value of the string from the example
above, and callbackk will take the value of the callback argument from
above.

Thus, the method that you've defined has three parameters but only
takes two explicit arguments.

[toc] | [prev] | [next] | [standalone]


#99757

Fromfl <rxjwg98@gmail.com>
Date2015-11-30 10:55 -0800
Message-ID<53ef7ef4-cfec-4c91-a45c-5b847dde8fad@googlegroups.com>
In reply to#99745
On Monday, November 30, 2015 at 12:02:57 PM UTC-5, Ian wrote:
> On Mon, Nov 30, 2015 at 10:44 AM, fl <ail.com> 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)
> >
> > The content inside parenthesis in last line is strange to me.
> >
> > "button %s" % i, callback
> 
> These are the arguments being passed to UI.Button. The first argument is:
> 
>     "button %s" % i
> 
> This is an example of printf-style string formatting. See the link
> that Zachary posted.
> 
> The second argument is the function named callback.
> 
> > That is, the writing looks like recognized as three items when I try with a
> > class definition (it can run with this):
> >
> > class buibutton():
> >     print 'sd'
> >     def __nonzero__(self):
> >        return False
> >
> >     def Button(str, ii, callbackk):
> >
> >         return
> >
> >
> > Could you explain it to me?
> 
> How is this related to the example above?
> 
> Here, Button is defined as a method of a class. Since it's a method,
> the first parameter is the "self" parameter, which will implicitly
> take the value of the class instance that you're calling the Button
> method on. If you're trying to call this like above, then the second
> parameter "ii" will take the value of the string from the example
> above, and callbackk will take the value of the callback argument from
> above.
> 
> Thus, the method that you've defined has three parameters but only
> takes two explicit arguments.

"How is this related to the example above? 

Here, Button is defined as a method of a class. Since it's a method, 
the first parameter is the "self" parameter, which will implicitly 
take the value of the class instance that you're calling the Button 
method on."

Thanks Ian. I created the class because I want to use the original example
line 

 UI.Button("button %s" % i, callback)

Is there another way to use the above line without my class definition?
I do feel that my created class does not match well with the above line
because the first item "button %s" does not fit __self__ in the class.
My understanding about the above line code may not correct. This may further
result in not the original bug pops up.

Thanks,


 

[toc] | [prev] | [next] | [standalone]


#99763

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-11-30 20:44 -0500
Message-ID<mailman.50.1448934253.14615.python-list@python.org>
In reply to#99757
On Mon, 30 Nov 2015 10:55:23 -0800 (PST), fl <rxjwg98@gmail.com> declaimed
the following:

>Thanks Ian. I created the class because I want to use the original example
>line 
>
> UI.Button("button %s" % i, callback)
>
>Is there another way to use the above line without my class definition?
>I do feel that my created class does not match well with the above line
>because the first item "button %s" does not fit __self__ in the class.

	The first item passed to a method call is the instance object... In
this case, whatever "UI" is bound to.

	If it helps, think of 

		UI.Button("string", callback)
as
		Button(UI, "string", callback)

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#99804

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-01 09:53 -0600
Message-ID<mailman.77.1448985262.14615.python-list@python.org>
In reply to#99757
On Mon, Nov 30, 2015 at 7:44 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Mon, 30 Nov 2015 10:55:23 -0800 (PST), fl <rxjwg98@gmail.com> declaimed
> the following:
>
>>Thanks Ian. I created the class because I want to use the original example
>>line
>>
>> UI.Button("button %s" % i, callback)
>>
>>Is there another way to use the above line without my class definition?
>>I do feel that my created class does not match well with the above line
>>because the first item "button %s" does not fit __self__ in the class.
>
>         The first item passed to a method call is the instance object... In
> this case, whatever "UI" is bound to.
>
>         If it helps, think of
>
>                 UI.Button("string", callback)
> as
>                 Button(UI, "string", callback)

This is only correct if "UI" is bound to an instance of a class and
"Button" is a method of that class. If UI is a class itself or a
module, then those are not equivalent.

[toc] | [prev] | [next] | [standalone]


#99751

Fromfl <rxjwg98@gmail.com>
Date2015-11-30 09:36 -0800
Message-ID<f18de0eb-07f1-424a-90c3-62fbc9be128a@googlegroups.com>
In reply to#99741
On Monday, November 30, 2015 at 11:44:44 AM UTC-5, fl wrote:
> Hi,
> 
> I come across the following code snippet.
> 
> 
> 
> 
> 
> for i in range(10):
>     def callback():
>         print "clicked button", i
>     UI.Button("button %s" % i, callback)
> 
> 
> 
> 
> The content inside parenthesis in last line is strange to me. 
> 
> "button %s" % i, callback
> 
> 
> That is, the writing looks like recognized as three items when I try with a
> class definition (it can run with this):
> 
> class buibutton():
>     print 'sd'
>     def __nonzero__(self):
>        return False
>        
>     def Button(str, ii, callbackk):
>         
>         return
> 
> 
> Could you explain it to me?
> 
> The link is here:
> 
> http://effbot.org/zone/default-values.htm
> 
> Thanks,

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:

for i in range(10):
    def callback(i=i):


I have the output for both:


%run "C:/Users/CCS6_1_Tiva_C/Python_prj0/uibutton1.py"
sd
clicked button 0
clicked button 1
clicked button 2
clicked button 3
clicked button 4
clicked button 5
clicked button 6
clicked button 7
clicked button 8
clicked button 9

%run "C:\Users\CCS6_1_Tiva_C\Python_prj0\uibutton0.py"
sd
clicked button 0
clicked button 1
clicked button 2
clicked button 3
clicked button 4
clicked button 5
clicked button 6
clicked button 7
clicked button 8
clicked button 9

I don't know why it does not have the not expected format output:

sd
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9
clicked button 9


Thanks,

[toc] | [prev] | [next] | [standalone]


#99756

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-30 11:24 -0700
Message-ID<mailman.48.1448907905.14615.python-list@python.org>
In reply to#99751
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".

[toc] | [prev] | [next] | [standalone]


#99752

FromTerry Reedy <tjreedy@udel.edu>
Date2015-11-30 12:36 -0500
Message-ID<mailman.45.1448905059.14615.python-list@python.org>
In reply to#99741
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

[toc] | [prev] | [next] | [standalone]


#99755

Fromfl <rxjwg98@gmail.com>
Date2015-11-30 09:50 -0800
Message-ID<06c312b2-a137-46e1-b31c-c9c2224224ad@googlegroups.com>
In reply to#99752
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?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web