dynamic assigments

Chris Rebert clp2 at rebertia.com
Thu Mar 24 18:26:34 EDT 2011


On Thu, Mar 24, 2011 at 1:39 PM, Seldon <seldon at katamail.it> wrote:
> Hi, I have a question about generating variable assignments dynamically.

This can frequently be a code smell.

> I have a list of 2-tuples like this
>
> (
> (var1, value1),
> (var2, value2),
> .. ,
> )
>
> where var1, var2, ecc. are strings and value1, value2 are generic objects.
>
> Now, I would like to use data contained in this list to dynamically generate
> assignments of the form "var1 = value1", ecc where var1 is an identifier
> equal (as a string) to the 'var1' in the list.
>
> Is there a way to achieve this ?

Is there a reason you can't or don't want to use a dict or object
attributes instead?

Cheers,
Chris



More information about the Python-list mailing list