Well, wondering what is the suggested way (i.e. best practice) to pass some dictionary to function/method, modify it and "return" to the user?
For example, as dictionary is mutable, we can simply do:
my_dict = {
'a': 'b'
}
def foo(bar):
# Do something with `bar`...
...
bar['c'] = d # Update `bar`.
foo(my_dict)
# Do something with modified `my_dict`...
This is what I was doing mostly in the past.
But another, more explicit, way to get the same effect would be:
my_dict = {
'a': 'b'
}
def foo(bar):
# Do something with `bar`...
...
bar['c'] = 'd' # Update `bar`.
return bar
my_dict = foo(my_dict)
# Do something with modified `my_dict`...
This maybe follows better one of the most important Python principles: "Explicit is better than implicit.", but definitely requires little more writing (maybe unnecessarily).
Of course, there are also other approaches to achieve the same, but wondering which is preferred way between above two?
P.S. This is my first question ever on StackOverflow (for 10+ years in programming)... I tried to search for similar use-cases (as always), but incredible that nobody ever asked.
EDIT: Updated examples to be more specific.