Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulating pass-by-value behaviour in python

I would like to emulate the pass-by-value behaviour in python. In other words, I would like to make absolutely sure that the function I write do not modify user supplied data.

One possible way is to use deep copy:

from copy import deepcopy def f(data):     data = deepcopy(data)     #do stuff 

is there more efficient or more pythonic way to achieve this goal, making as few assumptions as possible about the object being passed (such as .clone() method)

Edit

I'm aware that technically everything in python is passed by value. I was interested in emulating the behaviour, i.e. making sure I don't mess with the data that was passed to the function. I guess the most general way is to clone the object in question either with its own clone mechanism or with deepcopy.

like image 572
Boris Gorelik Avatar asked May 10 '09 11:05

Boris Gorelik


People also ask

How do you pass by value in Python?

Call by reference vs call by valueWhile calling a function, when we pass values by copying variables, it is known as “Call By Values.” In this method, a variable itself is passed. A copy of the variable is passed in a call by value. Change in the variable also affects the value of the variable outside the function.

Is pass by value possible in Python?

Python uses pass-by-value. It is also true that all variables in Python are references to values, but that doesn't change the fact that the parameter passing is by value. The page itself acknowledges that Java passes by value, and non-primitive types in Java are stored as references.

Is Python passing by reference or value?

The two most widely known and easy to understand approaches to parameter passing amongst programming languages are pass-by-reference and pass-by-value. Unfortunately, Python is “pass-by-object-reference”, of which it is often said: “Object references are passed by value.”


1 Answers

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).

like image 50
dF. Avatar answered Sep 28 '22 07:09

dF.