Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Re: Python-list Digest, Vol 27, Issue 123

 
Thread Tools Search this Thread
Old 12-09-2005, 07:55 AM   #1
Default Re: Python-list Digest, Vol 27, Issue 123


Michael Williams wrote:

> Thanks, Heiko, I'll give this a try. In the meantime, I'll try to
> explain what exactly I mean.
>
> Basically, I want the ability to reference a variable just as I am
> able to set a variable (or attribute) on the fly. For instance, say
> the user has the following list in a text file:
>
> [butter, cream, eggs, toast, jam]


list of what? what's butter? a variable? a string? did you mean

["butter", "cream", "eggs", "toast", "jam"]

or did you mean something else? (probably something else, since
whatever the user has put in the text file seems to have a value
property in your later examples).

> I want to be able to loop through that and say the following:
>
> item.__setattr__(list[0].value,myclass())


that's spelled

setattr(item, list[0].value, myclass())

in python.

to add attributes for all items in the list, do:

for v in list:
setattr(item, v.value, myclass())

if the list contains strings, you can simply do:

for v in list:
setattr(item, v, myclass())

> At that point I have item.butter, but I don't want to have to know
> (or hardcode) this, I want to then be able to do the following:
>
> item.__getattr__(list[0].value).__setattr__(list[1].value)


so the list is in fact a path? how about

this = item
for v in list[:-1]:
this = getattr(this, v)
setattr(this, list[-1], myclass())

hope this helps!

</F>





Fredrik Lundh
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Digital DIGEST - LIVE UPDATE Issue 40 Ablang DVD Video 0 12-15-2003 02:45 PM
Digital DIGEST - LIVE UPDATE Issue 39 Ablang DVD Video 0 11-29-2003 02:17 AM
Digital DIGEST - LIVE UPDATE Issue 38 Ablang DVD Video 0 11-09-2003 01:31 AM
DIGITAL DIGEST | LIVE UPDATE Issue 37 Ablang-Duff DVD Video 0 09-07-2003 05:24 AM
[liveupdate] Digest Number 29 Ablang DVD Video 0 07-05-2003 01:55 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46