Suppose there are 2 python modules - Library and application .
Library code :
class lib():
def __init__(self,arg):
smallArg = arg['key1']['key2']
smallArg['key3'] = 2
Application code :
class app():
def __init__(self):
self.arg = {'key1' : {'key2' : {'key3' : 1}}}
self.libObj = lib(self.arg)
def getArg(self):
print(self.arg)
appObj = app()
app.getArg()
since dictionaries are mutable , this prints the dictionary {'key1' : {'key2' : {'key3' : 2}}} which was changed by library code. This means library could even add more keys or delete them , and application would also get that mutated dictionary.
is there a way/practice to really ensure that the other modules don't disturb the structure of the dictionary passed ??
---> using copy.deepcopy() or json.loads(json.dumps()) doesn't seem the right way. There must be a better way !
This is just a small example where 2 modules interact with each other. Think about several more modules passing nested dictionaries to each other , computing them and returning them.Any module can change the structure of the nested dictionary. There is a good chance of missing out this detail and might break the code.
Is there a better way ? There must be.