changing local namespace of a function

Bo Peng bpeng at rice.edu
Fri Feb 4 15:27:42 EST 2005


Dear list,

I have many dictionaries with the same set of keys and I would like to 
write a function to calculate something based on these values. For 
example, I have

a = {'x':1, 'y':2}
b = {'x':3, 'y':3}

def fun(dict):
   dict['z'] = dict['x'] + dict['y']

fun(a) and fun(b) will set z in each dictionary as the sum of x and y.

My function and dictionaries are a lot more complicated than these so I 
would like to set dict as the default namespace of fun. Is this 
possible? The ideal code would be:

def fun(dict):
   # set dict as local namespace
   # locals() = dict?
   z = x + y

Many thanks in advance.
Bo



More information about the Python-list mailing list