compile() question

Jason Orendorff jason at jorendorff.com
Wed Dec 19 18:31:34 EST 2001


> I am parsing a stream of colon-delimited fields (field:value) where
> the field is an attribute for an object.  Since there are a few dozen
> fields and I don't want to handle each field separately, I used exec
> to make it easy (after splitting):
> 
>   exec ( 'volume.' + field + ' = "' + value + '"' )

This is a slow approach (plus it fails if there are " characters
in the value).  Try this instead:

  setattr(volume, field, value)

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list