dictionary update

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    dictionary update

    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).

    thx Chris
  • Sion Arrowsmith

    #2
    Re: dictionary update

    Chris <cfriedl@bigpon d.net.auwrote:
    >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(local s()[i])

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

    --
    \S -- siona@chiark.gr eenend.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

    Comment

    • Steve Holden

      #3
      Re: dictionary update

      Chris 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).
      >
      thx Chris
      Well, I'm note sure you have explained the problem fully, but if what
      you mean is that you want to update an instance's variables from a
      dictionary this can be achieved with

      self.__dict__.u pdate(xds)

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://holdenweb.blogspot.com
      Recent Ramblings http://del.icio.us/steve.holden

      Comment

      Working...