[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: Question about code writing '% i, callback'
From: fl <rxjwg98 () gmail ! com>
Date: 2015-11-30 18:55:23
Message-ID: 53ef7ef4-cfec-4c91-a45c-5b847dde8fad () googlegroups ! com
[Download RAW message or body]
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,
--
https://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic