Re: Newbie Q: dynamically assigning object attribute

2006-02-10 Thread Ben Wilson
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 TIMTOWTDY approach to expect the
one-ring-to-bind-them-all solution. :-) It's a mental shift on my part,
to be certain.

What I was actually doing was reading a user configuration file and
setting an object's variables--the example I got was a fairly close
approximation of how I was trying to approach it before setattr().

Thanks,
Ben

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie Q: dynamically assigning object attribute

2006-02-10 Thread Sion Arrowsmith
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-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 -- [EMAIL PROTECTED] -- 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Dylan Moreland
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


Re: Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Ben Wilson
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


Re: Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Dylan Moreland
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


Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Ben Wilson
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

-- 
http://mail.python.org/mailman/listinfo/python-list