Well, my Perl way of doing it would be to have all attributes in a dict
(hash), then create the accessor vi a dynamic function. I knew Python
would have a right way to do it for Python, but when I went looking I
neglected to look at the core of the language. I suppose I'm just too
accustomed to the
Ben Wilson <[EMAIL PROTECTED]> 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-
No problem. There are, in fact, ugly class-based methods of doing this.
You could assign to an instance's __dict__ dictionary, or -- if the
class is a new-style class -- you can call the __setattr__(self, name,
value) method.
--
http://mail.python.org/mailman/listinfo/python-list
That's it. I should spend more time R-ingTFM. I kept looking for some
class-based solution and there was a built in. Perfectly logical.
Thanks,
Ben
--
http://mail.python.org/mailman/listinfo/python-list
Take a look at the built-in function setattr:
http://ftp.python.org/doc/lib/built-in-funcs.html#l2h-64
--
http://mail.python.org/mailman/listinfo/python-list
I would like to dynamically assign object attributes:
dict = {
a : 1,
b : 2,
}
for key,val in dict :
obj.key = val
To get:
print obj.a
1
I've googled to no effect, or maybe I'm needing to be hit with the
appropriately sized clue-by-four. Any assistance would be appreciated.
Regards,
Ben