[Tutor] passing by reference

Steven D'Aprano steve at pearwood.info
Sun Mar 13 14:06:56 CET 2011

Vineeth Mohan wrote:
> Hi,
> How are variables in python passed. By value or by referrence?  


What makes you think that value and reference are the only two choices? 
See this Wikipedia article for a list of at least a dozen different 
argument passing techniques:


Three months ago I answered a similar question. You could do worse than 
read this:


> Is the following the only way to pass by reference? or is there any 
> other way

To mimic the behaviour of pass-by-reference, it is more common to pass a 
single-element list:

 >>> def modify(arg):
...     arg[0] = 23
 >>> var = [42]
 >>> modify(var)
 >>> assert var[0] == 23

but using instance attributes also works.


More information about the Tutor mailing list