Python Object Question
Jose' Sebrosa
sebrosa at artenumerica.com
Sat Apr 28 20:13:16 EDT 2001
Jason Joy wrote:
>
> Hello,
>
> I am having a bit of trouble with the following piece of code...
>
> result = objectChunk()
> masterkeys = output.keys()
> for key in masterkeys:
> result.key = output[key]
> ## I might have to do something different above...
> data.append(result)
>
> Where result.key is, of course, taken literally as result.key, not depending
> on what the variable key is. Does anyone know how I can make it use the
> actual value of "key"?
Try to use setattr: replace the line
result.key = output[key]
*exactly* by
setattr(result, key, output[key])
Also maybe you would like to check the {}.items() method of dictionaries. If
you dont need to use the "masterkeys" later, you can drop it like here:
result = objectChunk()
## masterkeys = output.keys()
for key, value in output.items():
setattr(result, key, value)
data.append(result)
Sebrosa
More information about the Python-list
mailing list