Pass variable by reference

Terry Reedy tjreedy at udel.edu
Tue May 6 15:16:54 EDT 2014


On 5/5/2014 8:39 PM, Satish Muthali wrote:
> Hello experts,
>
> I have a burning question on how to pass variable by reference in
> Python.

Python passes objects to functions. CPython implements this by passing 
object pointers. In one sense, your request is impossible. In another, 
it already happens.

> I understand that  the data type has to be mutable.

In C, all data are mutable, so copying a block of memory versus passing 
a pointer is the meaningful distinction. In Python, mutable arguments 
can be mutated and immutable args cannot, so *that* is the only 
meaningful distinction. Strings are not mutable.

> For example, here’s the issue I am running in to:
>
> I am trying to extract the PostgreSQL DB version for example:
>
> /pgVer = [s.split() for s in os.popen("psql
> --version").read().splitlines()]/
> /    print pgVer[0]/
> /    for i, var in enumerate(pgVer[0]):/
> /   if i == len(pgVer[0]) - 1:/
> /   pgversion = var/
> /
> /
> I would now like to pass ‘pgversion’ (where the value of pgversion is
> 9.3.4) by reference, for example:
>
> I want to nuke /var/lib/postgresql/9.3.4/main/data , however
> programatically I want it to be as:  /var/lib/postgresql/*/<value of
> pgversion>/*/main/data

It is not clear whether you want to delete a file or rename it. Either 
is easy and neither depend on passing mechanism.

-- 
Terry Jan Reedy





More information about the Python-list mailing list