Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'assign': 0.05; 'dictionary': 0.07; 'python?': 0.07; 'python': 0.07; 'referenced': 0.09; 'pm,': 0.11; 'this:': 0.11; 'wrote:': 0.14; 'baxter': 0.16; 'indirect': 0.16; '16,': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'variable': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'subject:question': 0.22; 'cc:addr :python-list': 0.22; 'mon,': 0.22; 'received:209.85.213': 0.23; 'similarly': 0.23; 'somehow': 0.23; 'value.': 0.25; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'hi,': 0.29; 'cc:addr:python.org': 0.31; 'andy': 0.31; 'skip:\xc2 20': 0.31; '...': 0.32; '(for': 0.33; 'lines': 0.34; 'using': 0.34; 'skip:" 10': 0.34; 'there': 0.35; 'point': 0.35; 'assignment': 0.35; 'doing': 0.36; 'some': 0.37; 'refer': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'less': 0.38; 'skip:" 20': 0.38; 'set': 0.39; 'skip:s 30': 0.39; 'where': 0.39; 'received:209': 0.39; '8bit%:8': 0.39; 'would': 0.40; 'header:Received:5': 0.40; '8bit%:9': 0.60; '8bit%:14': 0.60; 'choose': 0.61; '2011': 0.62; 'skip:\xc2 10': 0.83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=AStqlWAglUfKmlxBDgk3Mw6T6ZHsVz1AD8QSNjJG+D8=; b=MAnPYYCP+uU300zszZn2GrvqWtYNaRnfCTQxSuYGrIfMgKR8Xj9UmHMvXHk1fMly2X SR0OFhg39rSZM9M46aE78j9WNc+NHefho44VuYMBy0+wiSapnQuOdDsxYAAyZo9S681h NtMYkk6B3ybnc3lImkYwQ9jVfNuGfUd9YeCEQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=fn1JzCVix5doGrAe+oQ4hV1V/j9yFAmepy0lM0/pOrsS5X+5BYyY3QIPqF2s9rk4t5 XusMYKr9o/nszeF3Q+8iy0CoprXBFwpNCZboGU7Tc3fl1CZGmB7TkQRE8qxqHsfxc8lm dh1V7f/pxvwqQJtsQPkqm1HQTnsKQ2Y0VKUh4= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4DD203F1.1000202@gmail.com> References: <4DD203F1.1000202@gmail.com> Date: Mon, 16 May 2011 22:24:34 -0700 X-Google-Sender-Auth: lBk57TgicJqy9Ed5shXkeU6Klxg Subject: Re: indirect assignment question From: Chris Rebert To: Andy Baxter Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305609877 news.xs4all.nl 49174 [::ffff:82.94.164.166]:36918 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5550 On Mon, May 16, 2011 at 10:13 PM, Andy Baxter wrote: > Hi, > > I have some lines of code which currently look like this: > > =C2=A0 =C2=A0 =C2=A0self.window =3D self.wTree.get_widget("mainWindow") > =C2=A0 =C2=A0 =C2=A0self.outputToggleMenu =3D self.wTree.get_widget("menu= item_output_on") > =C2=A0 =C2=A0 =C2=A0self.outputToggleButton =3D self.wTree.get_widget("bu= tton_toggle_output") > =C2=A0 =C2=A0 =C2=A0self.logView =3D self.wTree.get_widget("textview_log"= ) > =C2=A0 =C2=A0 =C2=A0self.logScrollWindow =3D self.wTree.get_widget("scrol= ledwindow_log") > > and I would like (for tidiness / compactness's sake) to replace them with > something like this: > =C2=A0 =C2=A0 =C2=A0 =C2=A0widgetDic =3D { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "mainWindow": self.window, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "menuitem_output_on": self.outputToggl= eMenu, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "button_toggle_output": self.outputTog= gleButton, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "textview_log": self.logView, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "scrolledwindow_log": self.logScrollWi= ndow > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > =C2=A0 =C2=A0 =C2=A0 =C2=A0for key in widgetDic: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ... set the variable in dic[key] to po= int to > self.wTree.get_widget(key) somehow > > what I need is some kind of indirect assignment where I can assign to a > variable whose name is referenced in a dictionary value. > > Is there a way of doing this in python? You can achieve almost the same level of brevity, with less use of magic, by simply using a local variable to refer to self.wTree.get_widget: w =3D self.wTree.get_widget # or choose some other similarly short variable= name self.window =3D w("mainWindow") self.outputToggleMenu =3D w("menuitem_output_on") self.outputToggleButton =3D w("button_toggle_output") self.logView =3D w("textview_log") self.logScrollWindow =3D w("scrolledwindow_log") Python functions/methods are first-class; exploit this feature! Cheers, Chris -- http://rebertia.com