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


Groups > comp.lang.python > #99756

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

Path csiph.com!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:24:23 -0700
Lines 30
Message-ID <mailman.48.1448907905.14615.python-list@python.org> (permalink)
References <25af8ac3-5fc7-44bd-a73f-7a870b69515a@googlegroups.com> <f18de0eb-07f1-424a-90c3-62fbc9be128a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de sGDZcHHJOepImsoCQJFIHgirYXsCEt5eQUrWcAXtTnCQ==
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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Question': 0.05; 'subject:code': 0.07; 'callback': 0.09; 'expected.': 0.09; 'stored': 0.10; 'output': 0.13; 'def': 0.13; 'button(self,': 0.16; 'callback)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:writing': 0.16; 'values:': 0.16; 'wrote:': 0.16; '2015': 0.20; '%s"': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'loop,': 0.29; 'code:': 0.29; 'print': 0.30; 'skip:_ 10': 0.32; 'says': 0.32; 'point': 0.33; 'changed': 0.33; 'class': 0.33; 'received:google.com': 0.35; 'false': 0.35; 'i.e.': 0.35; 'nov': 0.35; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'thanks': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'called': 0.40; "you'll": 0.61; 'skip:u 10': 0.61; 'body': 0.61; '30,': 0.63; 'binding': 0.66; 'replies.': 0.84; '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=RDepqnY2KxTeKyYt9eWaj1aWpxmhY8qvhhDabSAC6bU=; b=Um8JHrNd+dzJnRktMkWTRwSIh1iaDwx7DLvB2YEc1Ng1RwihjRwk4/ZAl8sjZ4Eicv wMqEeLeE11kJJzubacIbDArKlkjQkzdwMuiVcoNPJneQ4BbH1R3BpR5+OFLnGUP4hugB s7diCoid+zTkDIefuo1R6ofrf8vSEmVRz/LZ0U36dzheB2pU4D5gUD+qxENwkpBKef29 sVsF6RiUE/PaNAIRnoix2zmH2Okt1gzVMXE5RR3lj9gUEumNbnb5smgWTEWw4dniBsjL 8zA61QOG9367YT7RsYLMUZ1MrfCIhVAE4gCb68dX6CDIUi/Cbn7bgMdR/n+sIJP+Z5K6 vBdw==
X-Received by 10.107.137.226 with SMTP id t95mr57784007ioi.188.1448907902518; Mon, 30 Nov 2015 10:25:02 -0800 (PST)
In-Reply-To <f18de0eb-07f1-424a-90c3-62fbc9be128a@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:99756

Show key headers only | View raw


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".

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