zljubi...@gmail.com wrote at 2020-6-14 08:20 -0700: >Hi, > >consider this example: > >from typing import Dict, List > > >class chk_params: > def execute(self, params: Dict[str, List] = None): > if params is None: > params = {} > > for k, v in params.items(): > params[k] = [val + 10 for val in v] > > return params.values() > >params = { > 'A' : [1], > 'B': [2], > 'C': [3], > 'D': [4], > 'E': [5] >} >print(params) >params_obj = chk_params() > >print(params_obj.execute(params = params)) >print(params) > >Output is: >{'A': [1], 'B': [2], 'C': [3], 'D': [4], 'E': [5]} >dict_values([[11], [12], [13], [14], [15]]) >{'A': [11], 'B': [12], 'C': [13], 'D': [14], 'E': [15]} > >I expected that last print statement will show original parameters A=1, B=2... >but this is not the case.
You must distinquish between the "name -> object" binding of function arguments and modifications of the object. You change the binding via assignments to the name (in your case of the form "params = ..."). Those assignments affect only the local name (i.e. in the function), not bindings of the name name elsewhere. If you change the object itself ("params" is bound to) (in your case via "params[k] = ..."), then the object changes - and this change can be seen at other places where this object is accessible. If you want object modifications made in your function not be seen elsewhere, you must work on a local object (not accessible elsewhere: e.g. def execute(self, params: Dict[str, List] = None): params = {} if params is None else params.copy() # copies ... -- https://mail.python.org/mailman/listinfo/python-list