Re: Pass by reference or by value?



On Thursday 16 August 2007, Robert Dailey wrote:
Hi,

I previously created a topic named "Pass by reference or by value" where I
inquired on how python's function parameters work. I received a lot of nice
responses, however I'm still confused on the topic. Note that I come from a
C++ background to Python, so any comparisons to C++ would be very helpful.

Very short answer:

Think of normal objects as always-pointers. There are no references. param = 5
sets the local variable "param" (be that of imaginary type int* or int, I
don't care) to whatever 5 is. This does not call an operator=, this plain
overwrites the variable.

If you want to change arguments in that way, you can use a list as an ugly
hack:

def foo(param):
param[0] = 5
print param[0]

a = [4]

foo(a)

yeah, I said ugly. That hack makes sure that a method of the passed object is
called instead of of the local namespace dict. (please note that I'm throwing
around abstract concepts without caring about an implementation).

--
Regards, Thomas Jollans
GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
Hacker key <http://hackerkey.com/>:
v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6

Attachment: signature.asc
Description: This is a digitally signed message part.



Relevant Pages

  • Re: termcap under single luser
    ... Note the termcap files do reference some other ... but it doesn't seem to matter much. ... reasonable looking hack. ...
    (freebsd-current)
  • Re: Pass by reference or by value?
    ... param = 5 sets the local variable "param" to whatever 5 is. ... The local variable "param" doesn't *have* a type, and can be a reference to a value of any type. ... That hack makes sure that a method of the passed object is called instead of of the local namespace dict. ... (please note that I'm throwing around abstract concepts without caring about an implementation). ...
    (comp.lang.python)
  • Re: PromptDataSource method
    ... Add a reference to Microsoft.Data.ConnectionUI. ... Microsoft.Data.ConnectionUI.DataConnectionDialog dcd = new ... But what the hack. ... Some features ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is it possible to have two Main() in one solution?
    ... the command-line compiler to add a reference to an executable, ... get it to work in VS.NET 2003 you have to hack the project file a bit. ...
    (microsoft.public.dotnet.languages.csharp)