Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly 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: 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 sGDZcHHJOepImsoCQJFIHgirYXsCEt5eQUrWcAXtTnCQ== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99756 On Mon, Nov 30, 2015 at 10:36 AM, fl 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".