I have a function that modifies 2 mutable objects (a list and a dict).
Given the nature of python If I modify them inside the function then the changes are supposed to be visible from outside.
My question is what convention/best practise says about returning values?
my_dict = {
'flag' : False
}
my_list = [
{'color': 'blue'},
{'color': 'yellow'}
]
def modify_in_place(my_dict_param, my_list_param):
my_dict_param['flag'] = True
for item in my_list_param:
item['color'] = 'red'
# nothing in this case
return
modify_in_place(my_dict, my_list)
# Now both arguments should reflect their modified states
Is the above code considered the best way to do this, or would this be "better"?
def modify_in_place(my_dict_param, my_list_param):
my_dict_param['flag'] = True
for item in my_list_param:
item['color'] = 'red'
return my_dict_param, my_list_param
my_dict, my_list = modify_in_place(my_dict, my_list)
In this case I'm returning the input (modified) values that will overwrite the original name references.
The result is supposed to be the same in both cases.
I'm aware that "better than" questions might be biased by opinions but given that python has often conventional ways to do things I'm asking about the "python way"