Dynamic variable creation from string

Massi massi_srb at msn.com
Wed Dec 7 12:09:16 EST 2011


Hi everyone,

in my script I have a dictionary whose items are couples in the form
(string, integer values), say

D = {'a':1, 'b':2, 'c':3}

This dictionary is passed to a function as a parameter, e.g. :

def Sum(D) :
    return D['a']+D['b']+D['c']

Is there a way to create three variables dynamically inside Sum in
order to re write the function like this?

def Sum(D) :
    # Here some magic to create a,b,c from D
    return a+b+c

It is really important that the scope of a,b,c is limited to the Sum
function, they must not exisit outside it or inside any other nested
functions.
Thanks in advance for your help!




More information about the Python-list mailing list