views:

61

answers:

1

I have a layer which helps me populating records from the form to tables and viceversa, it does some input checking, etc.

Now several methods of this layer which are called several times in different parts of the webform take the same parameters, so I wanted to pack them at the begining of the codefile.

kwargs(): return {"tabla":"nombre_tabla","id":[hf_id.Value] ,"container": Panel1,"MsgBox1": MsgBox1}

then I call

IA.search(**kwargs)

but doing that way the values of the dictionary get fixed with the ones they had in the begining, and one of them is retrieved from a webcontrol so it needs to be dynamic. So I wrapped them in a function

def kwargs(): return {"tabla":"nombre_tabla", "id":[hf_id.Value] ,"container": Panel1,"MsgBox1": MsgBox1}

and then I call

IA.search(*kwargs()) IA.save(*kwargs())

etc.

and that way the value of the dictionary which comes from the webform (hf_id) is dynamic and not fixed. But I was wondering if in this case there is another way, a pythonic way, to get the values of the dictionary kwargs to be dynamic and not fixed

+1  A: 

Python objects are pointers (though they are not directly manipulatable by the user.)

So if you create a list like this:

>>> a = [1, 2, 3]

and then store it in a dictionary:

>>> b = { 'key': a, 'anotherkey': 'spam' }

you will find modifications to the value in the dictionary also modify the original list:

>>> b['key'].append(4)
>>> print b['key']
[1, 2, 3, 4]
>>> print a
[1, 2, 3, 4]

If you want a copy of an item, so that modifications will not change the original item, then use the copy module.

>>> from copy import copy
>>> a = [1, 2, 3]
>>> b['key'] = copy(a)
>>> print b['key']
[1, 2, 3]
>>> b['key'].append(4)
>>> print b['key']
[1, 2, 3, 4]
>>> print a
[1, 2, 3]
Thomas O
instead of using the copy module, you can do this: `b['key'] = a[:]`
jcao219
Good point. But copy works for all objects, using the [:] operator works only for lists.
Thomas O