Pass variable by reference
Rustom Mody
rustompmody at gmail.com
Tue May 6 00:32:23 EDT 2014
On Tuesday, May 6, 2014 6:09:44 AM UTC+5:30, Satish Muthali wrote:
> Hello experts,
> I have a burning question on how to pass variable by reference in Python.
Technically correct answer: You cant. But see below.
> I understand that the data type has to be mutable.
I dont know that mutability has any bearing on this
You can get mostly the effect of pass by reference by using:
- multiple values (tuple) return
- unpacking assignment
Like so:
>>> def foo(x,y):
... return x+1, y+3
...
>>> x,y= 1,2
>>> x,y=foo(x,y)
>>> x,y
(2, 5)
>>>
> 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
I dont really understand your example
More information about the Python-list
mailing list