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


Groups > comp.lang.python > #5549 > unrolled thread

indirect assignment question

Started byAndy Baxter <highfellow@gmail.com>
First post2011-05-17 06:13 +0100
Last post2011-05-17 09:15 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  indirect assignment question Andy Baxter <highfellow@gmail.com> - 2011-05-17 06:13 +0100
    Re: indirect assignment question Ben Finney <ben+python@benfinney.id.au> - 2011-05-17 16:01 +1000
    Re: indirect assignment question Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-05-17 09:15 +0200

#5549 — indirect assignment question

FromAndy Baxter <highfellow@gmail.com>
Date2011-05-17 06:13 +0100
Subjectindirect assignment question
Message-ID<mailman.1657.1305609210.9059.python-list@python.org>
Hi,

I have some lines of code which currently look like this:

       self.window = self.wTree.get_widget("mainWindow")
       self.outputToggleMenu = self.wTree.get_widget("menuitem_output_on")
       self.outputToggleButton = 
self.wTree.get_widget("button_toggle_output")
       self.logView = self.wTree.get_widget("textview_log")
       self.logScrollWindow = self.wTree.get_widget("scrolledwindow_log")

and I would like (for tidiness / compactness's sake) to replace them 
with something like this:
         widgetDic = {
            "mainWindow": self.window,
            "menuitem_output_on": self.outputToggleMenu,
            "button_toggle_output": self.outputToggleButton,
            "textview_log": self.logView,
            "scrolledwindow_log": self.logScrollWindow
         }
         for key in widgetDic:
            ... set the variable in dic[key] to point 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?

thanks,

andy baxter

-- 

http://highfellow.org

[toc] | [next] | [standalone]


#5551

FromBen Finney <ben+python@benfinney.id.au>
Date2011-05-17 16:01 +1000
Message-ID<87y625euh6.fsf@benfinney.id.au>
In reply to#5549
Andy Baxter <highfellow@gmail.com> writes:

> with something like this:
>         widgetDic = {
>            "mainWindow": self.window,
>            "menuitem_output_on": self.outputToggleMenu,
>            "button_toggle_output": self.outputToggleButton,
>            "textview_log": self.logView,
>            "scrolledwindow_log": self.logScrollWindow
>         }
>         for key in widgetDic:
>            ... set the variable in dic[key] to point 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.

for (name, value) in widgetDic.iteritems():
    setattr(self, name, value)

-- 
 \         “The double standard that exempts religious activities from |
  `\       almost all standards of accountability should be dismantled |
_o__)                   once and for all.” —Daniel Dennett, 2010-01-12 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#5555

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-05-17 09:15 +0200
Message-ID<iqt7bn$iie$1@r03.glglgl.eu>
In reply to#5549
Am 17.05.2011 07:13 schrieb Andy Baxter:

> self.window = self.wTree.get_widget("mainWindow")
> self.outputToggleMenu = self.wTree.get_widget("menuitem_output_on")
> self.outputToggleButton = self.wTree.get_widget("button_toggle_output")
> self.logView = self.wTree.get_widget("textview_log")
> self.logScrollWindow = self.wTree.get_widget("scrolledwindow_log")
>
> and I would like (for tidiness / compactness's sake) to replace them
> with something like this:
> widgetDic = {
> "mainWindow": self.window,
> "menuitem_output_on": self.outputToggleMenu,
> "button_toggle_output": self.outputToggleButton,
> "textview_log": self.logView,
> "scrolledwindow_log": self.logScrollWindow
> }
> for key in widgetDic:
> ... set the variable in dic[key] to point to self.wTree.get_widget(key)
> somehow

assignmap = (
     ('mainWindow', 'window'),
     ('menuitem_output_on', 'outputToggleMenu'),
     ('button_toggle_output', 'outputToggleButton'),
     ('textview_log', 'logView'),
     ('scrolledwindow_log', 'logScrollWindow'),
)
for name, selfname in assignmap:
     val = widgetDic[name] = self.wTree.get_widget(name)
     setattr(self, selfname, val)


HTH,

Thomas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web