Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70980 > unrolled thread

Re: Pass variable by reference

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-05-06 15:16 -0400
Last post2014-05-07 01:18 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Pass variable by reference Terry Reedy <tjreedy@udel.edu> - 2014-05-06 15:16 -0400
    Re: Pass variable by reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-07 01:18 +0000

#70980 — Re: Pass variable by reference

FromTerry Reedy <tjreedy@udel.edu>
Date2014-05-06 15:16 -0400
SubjectRe: Pass variable by reference
Message-ID<mailman.9706.1399406113.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#71000

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-05-07 01:18 +0000
Message-ID<536989d6$0$29965$c3e8da3$5496439d@news.astraweb.com>
In reply to#70980
On Tue, 06 May 2014 15:16:54 -0400, Terry Reedy wrote:

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

Pass *by* reference is not the same as pass *a* reference. Confusing 
variables, values and references to values is how you get Ruby 
programmers swearing that Ruby does pass by reference, and Java 
programmers swearing that Java does pass by value, for *exactly* the same 
behaviour that Python has.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web