indirect assignment question

A

Andy Baxter

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
 
T

Thomas Rachel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top