This can be an interesting use case for the decorator function. Something like that:
def pass_by_value(f): def _f(*args, **kwargs): args_copied = copy.deepcopy(args) kwargs_copied = copy.deepcopy(kwargs) return f(*args_copied, **kwargs_copied) return _f
pass_by_value takes the function f as input and creates a new function _f , which deeply copies all its parameters, and then passes them to the original function f .
Using:
@pass_by_value def add_at_rank(ad, rank): ad.append(4) rank[3] = "bar" print "inside function", ad, rank a, r = [1,2,3], {1: "foo"} add_at_rank(a, r) print "outside function", a, r
Output:
"inside function [1, 2, 3, 4] {1: 'foo', 3: 'bar'}" "outside function [1, 2, 3] {1: 'foo'}"
source share