Emulating pass-by-value behaviour in python

There is no pythonic way of doing this.

Python provides very few facilities for enforcing things such as private or read-only data. The pythonic philosophy is that "we're all consenting adults": in this case this means that "the function shouldn't change the data" is part of the spec but not enforced in the code.


If you want to make a copy of the data, the closest you can get is your solution. But copy.deepcopy, besides being inefficient, also has caveats such as:

Because deep copy copies everything it may copy too much, e.g., administrative data structures that should be shared even between copies.

[...]

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types.

So i'd only recommend it if you know that you're dealing with built-in Python types or your own objects (where you can customize copying behavior by defining the __copy__ / __deepcopy__ special methods, there's no need to define your own clone() method).


usually when passing data to an external API, you can assure the integrity of your data by passing it as an immutable object, for example wrap your data into a tuple. This cannot be modified, if that is what you tried to prevent by your code.


You can make a decorator and put the cloning behaviour in that.

>>> def passbyval(func):
def new(*args):
    cargs = [deepcopy(arg) for arg in args]
    return func(*cargs)
return new

>>> @passbyval
def myfunc(a):
    print a

>>> myfunc(20)
20

This is not the most robust way, and doesn't handle key-value arguments or class methods (lack of self argument), but you get the picture.

Note that the following statements are equal:

@somedecorator
def func1(): pass
# ... same as ...
def func2(): pass
func2 = somedecorator(func2)

You could even have the decorator take some kind of function that does the cloning and thus allowing the user of the decorator to decide the cloning strategy. In that case the decorator is probably best implemented as a class with __call__ overridden.


There are only a couple of builtin typs that work as references, like list, for example.

So, for me the pythonic way for doing a pass-by-value, for list, in this example, would be:

list1 = [0,1,2,3,4]
list2 = list1[:]

list1[:] creates a new instance of the list1, and you can assign it to a new variable.

Maybe you could write a function that could receive one argument, then check its type, and according that resulta, perform a builtin operation that could return a new instance of the argument passed.

As I said earlier, there are only a few builtin types, that their behavior is like references, lists in this example.

Any way... hope it helps.