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


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

Question about Pass-by-object-reference?

Started byfl <rxjwg98@gmail.com>
First post2014-07-22 12:04 -0700
Last post2014-07-23 01:56 +0000
Articles 7 on this page of 27 — 12 participants

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


Contents

  Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:04 -0700
    Re: Question about Pass-by-object-reference? Ned Batchelder <ned@nedbatchelder.com> - 2014-07-22 15:32 -0400
      Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:54 -0700
    Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 12:34 -0700
      Re: Question about Pass-by-object-reference? Peter Pearson <ppearson@nowhere.invalid> - 2014-07-22 20:35 +0000
        Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 13:46 -0700
          Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 15:17 -0700
            Re: Question about Pass-by-object-reference? Joel Goldstick <joel.goldstick@gmail.com> - 2014-07-22 18:26 -0400
            Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 15:33 -0700
            Re: Question about Pass-by-object-reference? Jerry Hill <malaclypse2@gmail.com> - 2014-07-22 19:06 -0400
          Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 15:31 -0700
            Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 15:40 -0700
        Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 16:00 -0700
          Re: Question about Pass-by-object-reference? emile <emile@fenx.com> - 2014-07-22 16:10 -0700
    Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-22 20:27 -0400
      Re: Question about Pass-by-object-reference? fl <rxjwg98@gmail.com> - 2014-07-22 18:49 -0700
        Re: Question about Pass-by-object-reference? Ben Finney <ben+python@benfinney.id.au> - 2014-07-23 11:59 +1000
          Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-23 05:35 +0000
            Re: Question about Pass-by-object-reference? Chris Angelico <rosuav@gmail.com> - 2014-07-23 16:07 +1000
            Re: Question about Pass-by-object-reference? Ben Finney <ben+python@benfinney.id.au> - 2014-07-23 16:25 +1000
            Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-23 18:51 -0400
              Re: Question about Pass-by-object-reference? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-24 03:22 +0000
                Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-24 05:05 +0000
      Re: Question about Pass-by-object-reference? Steven D'Aprano <steve@pearwood.info> - 2014-07-23 05:36 +0000
        Re: Question about Pass-by-object-reference? Terry Reedy <tjreedy@udel.edu> - 2014-07-23 18:32 -0400
      Re: Question about Pass-by-object-reference? alex23 <wuwei23@gmail.com> - 2014-07-25 15:20 +1000
    Re: Question about Pass-by-object-reference? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-23 01:56 +0000

Page 2 of 2 — ← Prev page 1 [2]


#75101

FromTerry Reedy <tjreedy@udel.edu>
Date2014-07-23 18:51 -0400
Message-ID<mailman.12250.1406155927.18130.python-list@python.org>
In reply to#75055
On 7/23/2014 1:35 AM, Steven D'Aprano wrote:
> On Wed, 23 Jul 2014 11:59:45 +1000, Ben Finney wrote:
>
>> fl <rxjwg98@gmail.com> writes:
>>
>>> On Tuesday, July 22, 2014 8:27:15 PM UTC-4, Terry Reedy wrote:
>>>> When you call a function, Python binds function parameter names to
>>>> argument objects in the function's local namespace, the same as in
>>>> name assignments. […]
>>>> Nothing is being 'passed'.

Non-human implementations copy object ids, but I was not writing at that 
level.

>>> Thanks, but I don't understand your point yet. Could you give me
>>> another example in which something is passed?
>>
>> The point being made is that no values are is “passed” in a function
>> call. If you have learned that term from elsewhere, it doesn't apply
>> sensibly to Python.
>
> Hmmm. I don't know that I like that. I think that these two sentences
> mean the same thing:
>
>      "Call the function with x as argument."
>
>      "Pass x to the function."

I disagree. In Python, when one calls the function with an argument, 
computer implementatipns 'pass' an object reference, which in CPython is 
an object address. Conflating the two sentences above leads people to 
claim that python 'calls by (object) reference'.  In a sense, it does, 
But thinking in those terms often leads to false inferences about how 
Python behaves.  So I think it better not to think is such terms.

> They both describe what is being done, only from slightly different
> points of view.

'Passing' is an implementation of calling. I have little idea what *I* 
do when interpreting a function call while interpreting python code.

> In mathematics, to call a function is a completely
> abstract action. Magic happens, and a result is returned.

 From a set theory point of view, nothing happens. The result simply is 
the second member of the pair with the argument as the first member.

 > But in programming languages, calling a function has concrete actions:

The *essential* concrete action is the binding of something to parameter 
names. 'Passing' is a means to that end. What matters is what gets 
bound. In Python, python objects themselves get bound. And the binding 
is essentially the same as in assignment statements. Other language bind 
values or references or pointers or ..., with different consequences.

-- 
Terry Jan Reedy


[toc] | [prev] | [next] | [standalone]


#75117

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-24 03:22 +0000
Message-ID<53d07be8$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#75101
On Wed, 23 Jul 2014 18:51:47 -0400, Terry Reedy wrote:

> On 7/23/2014 1:35 AM, Steven D'Aprano wrote:
>> On Wed, 23 Jul 2014 11:59:45 +1000, Ben Finney wrote:
>>
>>> fl <rxjwg98@gmail.com> writes:
>>>
>>>> On Tuesday, July 22, 2014 8:27:15 PM UTC-4, Terry Reedy wrote:

>>> The point being made is that no values are is “passed” in a function
>>> call. If you have learned that term from elsewhere, it doesn't apply
>>> sensibly to Python.
>>
>> Hmmm. I don't know that I like that. I think that these two sentences
>> mean the same thing:
>>
>>      "Call the function with x as argument."
>>
>>      "Pass x to the function."
> 
> I disagree. In Python, when one calls the function with an argument,
> computer implementatipns 'pass' an object reference, 

Terry, while I appreciate that you are trying to prevent confusion, I 
think you're actually causing even more confusion than what you are 
arguing against. Starting with yourself.

I stated that "call function with argument x" and "pass x to function" 
mean the same thing. You said that you disagree, and then the very next 
sentence you link the two concepts together: when you call a function 
with an argument, the computer passes something to the function in some 
manner. By your own description, one implies the other. If not, then I'd 
like to see an example of a programming language where you can have one 
without the other.

I respectfully disagree with you and Ben Finney on this matter. I think 
that the plain English meanings of the two sentences will be understood 
as the same thing by any English speaker old enough to have learned about 
functions (in mathematics or computing), and that there is no harm in 
this any more than it is harmful to treat these as the same:

    "I went for a stroll by the river."

    "I took a leisurely walk along the river."


> which in CPython is an object address.

If we're talking CPython implementation, then surely it passes a pointer, 
which is an abstract data type and not necessarily just an address. 
(Pointers in most languages that have them also have a type, not just a 
value.) But that's just a niggle.


> Conflating the two sentences above leads people to
> claim that python 'calls by (object) reference'.  In a sense, it does,
> But thinking in those terms often leads to false inferences about how
> Python behaves.  So I think it better not to think is such terms.

I don't agree that this is the cause of the problem. As I've stated many 
times now, I believe that the problem is that people believe that there 
are only two argument passing conventions, pass by value and pass by 
reference. Do you think it helps matters one iota if we refer to them as 
"call by value" and "call by reference" instead of "pass ..."?

I've never found anyone claiming that Python is call by name, even though 
the semantics of call by name are very similar to those of call by 
reference. I'd even argue that call by name is a *better* fit (still 
wrong, but not quite as wrong) as call by reference: the difference being 
that in call by reference semantics, the argument cannot be a literal, so 
we could not write this:

    some_function([])

but (as I understand it) with call by name semantics, you can. But 
because call by name is less well known than call by reference, people 
don't suggest it.

By the way, you refer to "call by object reference" as being a wrong 
inference. I think your terminology is messed up, and if you want to 
argue I suggest you take it up with the Effbot:

    The most accurate description is CLU’s “call by object” or “call
    by sharing“. Or, if you prefer, “call by object reference“.

http://effbot.org/zone/call-by-object.htm

Call by object reference would be a good name for what Python does (since 
it actually is what CPython does), if not for the unfortunate fact that 
if you drop the word "object" it leaves "call by reference", which of 
course is a different calling convention with well-defined, and very 
different, semantics. So I prefer "call by object" or "call by sharing".

But regardless of which name is used, we agree that that's what Python 
does (as well as Ruby and Java and others). This article:

http://javadude.com/articles/passbyvalue.htm


is a remarkable (and popular!) example of the confusion. Despite spending 
some time at the end of the article inventing his own terminology "pass 
by memento" and "pass by proxy" for remote method calls [and I'll get to 
them in a minute], the author spends the whole discussion implicitly 
assuming that call by value and call by reference are the only two ways 
that you can pass arguments to a function. This leads him to the 
remarkable conclusion that if you create a Dog instance and bind it a 
name myDog (in Java: Dog myDog; in Python, myDog = Dog()) the actual 
value of myDog is not the instance but some memory address, say 42.

You have to be very clever to be that stupid.

As Fredrik Lundh (the Effbot) wrote:

    well, I guess you can, in theory, value an artificial number 
    assigned to an object as much as the object itself.

        "Joe, I think our son might be lost in the woods"
        "Don't worry, I have his social security number"


The claim that myDog is actually a memory address is also factually 
incorrect (the author acknowledges this in a parenthetical aside, but I'm 
not going to let that get in the way of a good rant). Since the Java 
garbage collector can move blocks of memory around, a simple address 
isn't enough, it needs to be an indirect address managed by the garbage 
collector. Possibly something like what classic Mac OS called a handle.

As far as as his "call by memento", from the description that is 
*exactly* the same semantics as call by value, the only difference being 
that "copy the value" in this case requires serialising it and sending it 
across the network instead of merely copying bytes in memory. And his 
"call by proxy" sounds exactly like call by object except (again) the 
implementation requires using a proxy which references back to the 
original object. That is, it's an object reference, just not a managed 
pointer. All of which implies that Java's remote method calls use exactly 
the same argument passing strategy as it's non-remote method calls.

(You'll note I say the same *strategy*, not the same *implementation*.)


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#75121

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-24 05:05 +0000
Message-ID<53d093fd$0$2814$c3e8da3$76491128@news.astraweb.com>
In reply to#75117
On Thu, 24 Jul 2014 03:22:16 +0000, Steven D'Aprano wrote:

[...]
> I stated that "call function with argument x" and "pass x to function"
> mean the same thing.

Oh, a thought comes to mind. There is a sense in which "calling a 
function with argument x" is slightly different from "passing x to a 
function": when you call a function, the compiler has to do at least two 
things: 

1) record enough information somewhere (usually on a call stack) so 
   it can return back to the caller once the function returns;

2) record in some manner the arguments to the function where the 
   function can get to them ("pass the arguments").

So if you want to be pedantic and argue that calling does more than 
passing arguments, I'll have to concede the point that you are 
technically correct. But in the context of discussing the treatment of 
arguments passed to the function when you call a function, I think that's 
a distinction that matters not.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#75056

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-23 05:36 +0000
Message-ID<53cf49e5$0$29897$c3e8da3$5496439d@news.astraweb.com>
In reply to#75042
On Tue, 22 Jul 2014 20:27:15 -0400, Terry Reedy wrote:

> When you call a function, Python binds function parameter names to
> argument objects in the function's local namespace, the same as in name
> assignments. Given
> 
> def f(a, b): pass
> 
> a call f(1, 'x') starts by executing
> 
> a, b = 1, 'x'
> 
> in the local namespace.  Nothing is being 'passed'.


If nothing is being passed, how does the function know to bind 1 and 'x' 
to names a and b, rather than (say) this?

a, b = 23, 'Surprise!"


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#75100

FromTerry Reedy <tjreedy@udel.edu>
Date2014-07-23 18:32 -0400
Message-ID<mailman.12249.1406154767.18130.python-list@python.org>
In reply to#75056
On 7/23/2014 1:36 AM, Steven D'Aprano wrote:
> On Tue, 22 Jul 2014 20:27:15 -0400, Terry Reedy wrote:
>
>> When you call a function, Python binds function parameter names to
>> argument objects in the function's local namespace, the same as in name
>> assignments. Given
>>
>> def f(a, b): pass
>>
>> a call f(1, 'x') starts by executing
>>
>> a, b = 1, 'x'
>>
>> in the local namespace.  Nothing is being 'passed'.
>
>
> If nothing is being passed, how does the function know to bind 1 and 'x'
> to names a and b, rather than (say) this?
>
> a, b = 23, 'Surprise!"

To quote your other message, "Magic happens, and a result is returned."

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#75196

Fromalex23 <wuwei23@gmail.com>
Date2014-07-25 15:20 +1000
Message-ID<lqspes$1cv$1@dont-email.me>
In reply to#75042
On 23/07/2014 10:27 AM, Terry Reedy wrote:
> When you call a function, Python binds function parameter names to
> argument objects in the function's local namespace, the same as in name
> assignments. Given
>
> def f(a, b): pass
>
> a call f(1, 'x') starts by executing
>
> a, b = 1, 'x'
>
> in the local namespace.  Nothing is being 'passed'.

The Dude: Look, nothing is passed, here, man.
The Big Lebowski: Nothing is passed?  The god damn argument has crashed 
into the parameter!


...sorry, it's been a long week.

[toc] | [prev] | [next] | [standalone]


#75047

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-23 01:56 +0000
Message-ID<53cf1660$0$6574$c3e8da3$5496439d@news.astraweb.com>
In reply to#75013
On Tue, 22 Jul 2014 12:04:09 -0700, fl wrote:

> Hi,
> I learn Python function call on tutorial. There is a link on this
> subject.
> http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-
explained-by-philip-k-dick/
> 
> Although it explains clearly, the figure makes me puzzled.


Here is my take on the same question:

http://import-that.dreamwidth.org/1130.html

Feedback is welcome.


> ""Python is different. As we know, in Python, "Object references are
> passed by value".

I'm not sure who said that, but they weren't really being clear. The 
mention of "passed by value" is pointless, since it refers only to the 
implementation inside the interpreter, and not anything you can see using 
Python code. What it means is that under the hood, you have a mysterious 
thing called an "object reference". That's just another way of saying 
"reference to an object", which is usually implemented as a pointer. When 
the interpreter passes an argument to a function, it does so by making a 
copy of that pointer (hence "passed by value"). The copy still points to 
the same object.

I believe that for people thinking at the level of *Python code*, such 
descriptions aimed at the level of the underlying C code inside the 
interpreter add heat but no light. They simply cause more confusion than 
clarity.


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

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web