This is how Python works. In Python, variable names act like labels that 
are attached to objects in memory.

>>> export_get_vars = request.get_vars
>>> export_get_vars is request.get_vars
True
>>> id(request.get_vars)
54474840L
>>> id(export_get_vars)
54474840L

As you can see above, export_get_vars and request.get_vars are just two 
different labels that point to the same object in memory (a 
gluon.storage.Storage object in this case). If you mutate the object (in 
your case by adding elements to it), both variables will still refer to 
that same mutated object.

If you want to create a separate copy of request.get_vars that is an 
entirely new object, you have to do so explicitly:

import copy
export_get_vars = copy.copy(request.get_vars)

or

from gluon.storage import Storage
export_get_vars = Storage(request.get_vars)

or

export_get_vars = request.get_vars.copy()  # this returns a dictionary 
rather than a new Storage object

Note, the above options will only create a shallow copy -- if 
request.get_vars happens to contain an item that is a list, the copy will 
still point to the original list (i.e., the list itself will not be 
copied). There is a copy.deepcopy() function, but it doesn't work with 
Storage objects, so if you need a deep copy, you'll have to implement that 
manually.

Anthony

>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to