Newbie Q: dynamically assigning object attribute
Sion Arrowsmith
siona at chiark.greenend.org.uk
Fri Feb 10 13:03:32 EST 2006
Ben Wilson <dausha at gmail.com> wrote:
>I would like to dynamically assign object attributes:
>
>dict = {
> a : 1,
> b : 2,
>}
>
>for key,val in dict :
> obj.key = val
>
>I've googled to no effect, or maybe I'm needing to be hit with the
>appropriately sized clue-by-four.
The conventional clue-by-four applied to this question is "Don't
do that: just put the dictionary (or a copy of it) in the object
with obj.d = d (and don't shadow the built-in dict while you're
at it)." Having spent significant portions of the last two days
coping with the mess of adding functionality to a class written
by someone who thought doing what you want to do was a Good Idea,
I can only concur with that conventional wisdom.
Really. Don't do it. (Unless you have a damn good reason. In
which case, you've already been pointed at setattr().)
--
\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