Re: Question about code writing '% i, callback'
On Mon, Nov 30, 2015 at 7:44 PM, Dennis Lee Bieberwrote: > On Mon, 30 Nov 2015 10:55:23 -0800 (PST), fl 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
On Mon, Nov 30, 2015 at 10:44 AM, flwrote: > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
On Mon, Nov 30, 2015 at 10:44 AM, flwrote: > 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
On Mon, Nov 30, 2015 at 10:53 AM, Zachary Warewrote: > On Mon, Nov 30, 2015 at 10:44 AM, fl 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
On Monday, November 30, 2015 at 12:02:57 PM UTC-5, Ian wrote: > On Mon, Nov 30, 2015 at 10: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) > > > > 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, -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
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, -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about code writing '% i, callback'
On Mon, Nov 30, 2015 at 10:36 AM, flwrote: > 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". -- https://mail.python.org/mailman/listinfo/python-list