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


Groups > comp.lang.python > #99757

Re: Question about code writing '% i, callback'

X-Received by 10.66.154.132 with SMTP id vo4mr62249048pab.14.1448909724412; Mon, 30 Nov 2015 10:55:24 -0800 (PST)
X-Received by 10.50.50.237 with SMTP id f13mr328045igo.9.1448909724379; Mon, 30 Nov 2015 10:55:24 -0800 (PST)
Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!mv3no6541067igc.0!news-out.google.com!f6ni15085igq.0!nntp.google.com!mv3no8289680igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Mon, 30 Nov 2015 10:55:23 -0800 (PST)
In-Reply-To <mailman.41.1448902962.14615.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=50.100.117.144; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo
NNTP-Posting-Host 50.100.117.144
References <25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com> <mailman.41.1448902962.14615.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <53ef7ef4-cfec-4c91-a45c-5b847dde8fad@googlegroups.com> (permalink)
Subject Re: Question about code writing '% i, callback'
From fl <rxjwg98@gmail.com>
Injection-Date Mon, 30 Nov 2015 18:55:24 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:99757

Show key headers only | View raw


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,


 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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