dictionary update

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue Aug 15 08:41:53 EDT 2006


Chris  <cfriedl at bigpond.net.au> wrote:
>I have a instance attribute self.xds and a local variable xds in the 
>instance . Both are dictionaries. I understand that I can update 
>self.xds with xds as follows: self.xds.update(xds)
>
>However I have many instance attributes, self.i, and local variables i.
>I'd like to be able to do this:
>
>for i in list_of_i's:
>     self.i.update(i)
>
>How can this be done (I'm assuming it can be).

I think what you want is:

for i in list_of_is:
    getattr(self, i).update(locals()[i])

assuming list_of_is is a list of names not a list of the local
dicts (ie ["xds", ...] not [xds, ...]).

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list