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


Groups > comp.lang.python > #99745

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

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Question about code writing '% i, callback'
Date Mon, 30 Nov 2015 11:01:55 -0600
Lines 48
Message-ID <mailman.41.1448902962.14615.python-list@python.org> (permalink)
References <25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de M+ishJQYB7/ORtFmIoEmqgpdZyEVOfVj2Uiau2O/CLjQ==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Question': 0.05; 'method,': 0.07; 'subject:code': 0.07; 'callback': 0.09; 'posted.': 0.09; 'def': 0.13; 'argument': 0.15; 'above?': 0.16; 'callback)': 0.16; 'formatting.': 0.16; 'parameter,': 0.16; 'printf-style': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:writing': 0.16; 'wrote:': 0.16; 'string': 0.17; '2015': 0.20; '%s"': 0.22; 'arguments': 0.22; 'explicit': 0.22; 'parameter': 0.22; 'trying': 0.22; 'am,': 0.23; 'defined': 0.23; 'second': 0.24; 'recognized': 0.24; 'header:In- Reply-To:1': 0.24; 'mon,': 0.24; 'example': 0.26; 'parameters': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'looks': 0.29; '(it': 0.29; 'implicitly': 0.29; 'thus,': 0.29; 'print': 0.30; 'code': 0.30; 'class.': 0.30; 'skip:_ 10': 0.32; 'related': 0.32; 'run': 0.33; 'class': 0.33; 'definition': 0.34; 'me?': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'false': 0.35; 'instance': 0.35; 'nov': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'method': 0.37; 'received:209': 0.38; 'button': 0.38; 'takes': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; '30,': 0.63; 'above,': 0.63; 'strange': 0.63; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=lZ11fujKwyG29do8wdfiMSrDTooOBC80/TQN5ligPdM=; b=wVWtpvtYJEg41aifoGhrxwKlmHlQiWkSIME+yZRx5EnAcMX2ueLNiL8lIF+vlEQgRS gUn6qwCP87jENKo7p/z+PkBTmwdUUSPIU97Zw6K8OXwUSOsS++3rqCRcS5xBolGSViqA xkHkjMy+wUSsH6x3J9RRAkoBjUVaGKtR9rTcPsh+00Bb/TOUheOMEDCJzMHTblFr88/g Elvf3H2YMlJ+C1GrMHgFwFmz8qsg/Fbkg2p9wzLrFu2LCwsgpIOU6KHFoev1VDpo2skJ VmgBENFlFG/Q51w9JUJsikz5Rft9rO4GzdYgRRdKt0y9+9JuDWgoCBgkmqBuC+zfCgnf NOKQ==
X-Received by 10.107.137.226 with SMTP id t95mr57421735ioi.188.1448902954718; Mon, 30 Nov 2015 09:02:34 -0800 (PST)
In-Reply-To <25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99745

Show key headers only | View raw


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.

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