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 20 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 1 of 2  [1] 2  Next page →


#75013 — Question about Pass-by-object-reference?

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 12:04 -0700
SubjectQuestion about Pass-by-object-reference?
Message-ID<cc0616eb-642a-426c-a995-6665ee4c52b9@googlegroups.com>
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.

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

A function receives a reference to (and will access) the same object in memory as
used by the caller. However, it does not receive the box that the caller is
storing this object in; as in pass-by-value, the function provides its own box and
creates a new variable for itself. Let's try appending again:""

On the figure, it shows that the result is [0, 1]  (Am I right on the figure?)


When I enter the command lines on my computer:
>>> list=[0]
>>> append(list)
>>> print(list)
[0]

How to understand my result and that figure?



Thanks,

[toc] | [next] | [standalone]


#75014

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-07-22 15:32 -0400
Message-ID<mailman.12191.1406057555.18130.python-list@python.org>
In reply to#75013
On 7/22/14 3:04 PM, 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.
>
> ""Python is different. As we know, in Python, "Object references are passed by
> value".
>
> A function receives a reference to (and will access) the same object in memory as
> used by the caller. However, it does not receive the box that the caller is
> storing this object in; as in pass-by-value, the function provides its own box and
> creates a new variable for itself. Let's try appending again:""
>
> On the figure, it shows that the result is [0, 1]  (Am I right on the figure?)
>

This is a topic that often confuses people new to Python.  The article 
you linked to seems very confusing to me.  My own take on how to explain 
it is here: http://nedbatchelder.com/text/names.html

>
> When I enter the command lines on my computer:
>>>> list=[0]
>>>> append(list)
>>>> print(list)
> [0]
>
> How to understand my result and that figure?

You should have gotten [0, 1], you must have different code than was 
shown in the article.  I recommend putting the code into a .py file, and 
running it all at once.  Then if it doesn't do what you expect, you can 
show the entire .py file when asking for help.

>
>
>
> Thanks,
>


-- 
Ned Batchelder, http://nedbatchelder.com

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


#75016

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 12:54 -0700
Message-ID<a59553b7-3a3c-4096-9372-caabec8a68db@googlegroups.com>
In reply to#75014
On Tuesday, July 22, 2014 3:32:19 PM UTC-4, Ned Batchelder wrote:
> On 7/22/14 3:04 PM, fl wrote:
> it is here: http://nedbatchelder.com/text/names.html
> > When I enter the command lines on my computer:
> I recommend putting the code into a .py file, and 
> running it all at once.  Then if it doesn't do what you expect, 
> Ned Batchelder, http://nedbatchelder.com


Thanks Ned. You give a great helpful link. And your advice on .ph file is the
key problem of my OP (Thus I rewrite my post again).

Your link makes complicate things much easier. I read it carefully.

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


#75015

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 12:34 -0700
Message-ID<986eee35-0327-46e5-bce0-b1ae4572dd8f@googlegroups.com>
In reply to#75013
On Tuesday, July 22, 2014 3:04:09 PM UTC-4, fl wrote:
Hi, 
Excuse me. I find that the OP misses some info. I rewrite it again:

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. 

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

A function receives a reference to (and will access) the same object in memory as used by the caller. However, it does not receive the box that the caller is
storing this object in; as in pass-by-value, the function provides its own box 
and creates a new variable for itself. Let's try appending again:"" 

On the figure, I understand the figure about append function. Here is the lines:

When I enter the command lines on my computer: 
>>> def append(list):
... 	list.append(1)
... 	
>>> list=[0]
>>> append(list)
>>> print(list)
[0, 1]



But I don't understand the reassign function result:

>>> def reassign(list):
... 	list=[0,1]
... 
>>> list=[0]
>>> reassign(list)
>>> print list
[0]

Questions:
1. From the tutorial explanation, both function append and reassign use 
pass-by-object-reference. Is it right?

2. The tutorial says: "The caller doesn't care if you reassign the function's 
box. Different boxes, same content." about the reassign function. I don't
understand "Different boxes, same content". It uses different boxes (I agree).
But the results are different for the caller ([0]) and the function reassign 
([0, 1]).


What is wrong with my understanding? 



Thanks a lot, 

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


#75018

FromPeter Pearson <ppearson@nowhere.invalid>
Date2014-07-22 20:35 +0000
Message-ID<c3808lFn5iuU1@mid.individual.net>
In reply to#75015
On Tue, 22 Jul 2014 12:34:51 -0700 (PDT), fl <rxjwg98@gmail.com> wrote:
[snip]
>
> But I don't understand the reassign function result:
>
>>>> def reassign(list):
> ... 	list=[0,1]
> ... 
>>>> list=[0]
>>>> reassign(list)
>>>> print list
> [0]

When you say "def reassign(list)", that means "I'm defining a function
to which the caller will pass one object, and within this function I'm
going to refer to that object by the name 'list'."

Then, when you say "list=[0,1]", that means "Create the object [0,1],
and assign to it the name 'list'."  At this point, there is no longer
any name that refers to the object that the caller passed.

You might have thought that "list=[0,1]" would modify the caller-passed
object, but that's not what happens.  That's not what "=" means.

-- 
To email me, substitute nowhere->spamcop, invalid->net.

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


#75019

Fromemile <emile@fenx.com>
Date2014-07-22 13:46 -0700
Message-ID<mailman.12193.1406062021.18130.python-list@python.org>
In reply to#75018
On 07/22/2014 01:35 PM, Peter Pearson wrote:
> On Tue, 22 Jul 2014 12:34:51 -0700 (PDT), fl <rxjwg98@gmail.com> wrote:
> [snip]
>>
>> But I don't understand the reassign function result:
>>
>>>>> def reassign(list):
>> ... 	list=[0,1]
>> ...
>>>>> list=[0]
>>>>> reassign(list)
>>>>> print list
>> [0]
>
> When you say "def reassign(list)", that means "I'm defining a function
> to which the caller will pass one object, and within this function I'm
> going to refer to that object by the name 'list'."
>
> Then, when you say "list=[0,1]", that means "Create the object [0,1],
> and assign to it the name 'list'."  At this point, there is no longer
> any name that refers to the object that the caller passed.
>
> You might have thought that "list=[0,1]" would modify the caller-passed
> object, but that's not what happens.  That's not what "=" means.
>

However, if that is the behavior you were after, you can get there.


def reassign(mylist):  # no reason to shadow the list builtin
     mylist[:] = [0,1]

mylist = [1]
reassign(mylist)
mylist


Emile

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


#75031

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 15:17 -0700
Message-ID<6d919cfd-5d3e-4081-94a7-80ce64eedd83@googlegroups.com>
In reply to#75019
On Tuesday, July 22, 2014 4:46:25 PM UTC-4, emile wrote:
> On 07/22/2014 01:35 PM, Peter Pearson wrote:
> def reassign(mylist):  # no reason to shadow the list builtin
>      mylist[:] = [0,1]
> mylist = [1]
> reassign(mylist)
> mylist
> Emile

Thanks for your example. I do not find the explanation of [:] on line. Could you
explain it to me, or where can I find it on line?

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


#75035

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-07-22 18:26 -0400
Message-ID<mailman.12203.1406068020.18130.python-list@python.org>
In reply to#75031

[Multipart message — attachments visible in raw view] — view raw

it copies the list


On Tue, Jul 22, 2014 at 6:17 PM, fl <rxjwg98@gmail.com> wrote:

> On Tuesday, July 22, 2014 4:46:25 PM UTC-4, emile wrote:
> > On 07/22/2014 01:35 PM, Peter Pearson wrote:
> > def reassign(mylist):  # no reason to shadow the list builtin
> >      mylist[:] = [0,1]
> > mylist = [1]
> > reassign(mylist)
> > mylist
> > Emile
>
> Thanks for your example. I do not find the explanation of [:] on line.
> Could you
> explain it to me, or where can I find it on line?
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

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


#75037

Fromemile <emile@fenx.com>
Date2014-07-22 15:33 -0700
Message-ID<mailman.12204.1406068419.18130.python-list@python.org>
In reply to#75031
On 07/22/2014 03:17 PM, fl wrote:
> On Tuesday, July 22, 2014 4:46:25 PM UTC-4, emile wrote:
>> On 07/22/2014 01:35 PM, Peter Pearson wrote:
>> def reassign(mylist):  # no reason to shadow the list builtin
>>       mylist[:] = [0,1]
>> mylist = [1]
>> reassign(mylist)
>> mylist
>> Emile
>
> Thanks for your example. I do not find the explanation of [:] on line.

It's covered in the tutorial in

https://docs.python.org/2/tutorial/introduction.html

look for the section on slice notation

> Could you explain it to me, or where can I find it on line?

If you haven't already, you should work your way through the full 
tutorial if for no other reason that to be familiar with content others 
find beginners should be introduced to.

https://docs.python.org/2/tutorial/index.html

Emile


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


#75040

FromJerry Hill <malaclypse2@gmail.com>
Date2014-07-22 19:06 -0400
Message-ID<mailman.12206.1406070378.18130.python-list@python.org>
In reply to#75031
On Tue, Jul 22, 2014 at 6:17 PM, fl <rxjwg98@gmail.com> wrote:
> Thanks for your example. I do not find the explanation of [:] on line. Could you
> explain it to me, or where can I find it on line?

It's pretty hard to find if you don't already know what's going on.

First, you need to know that mylst[i:j] refers to a slice of  the list
"mylist".  Specifically, the items from the list starting with the
item at index i, up to but not including the item at index j.  If you
leave the i off (e.g., mylist[:j]) that's a slice from the start of
the list up to (but not including) the j-th item.  If you leave the
end position off, (e.g., mylist[i:]), that gets you the i-th item to
the end (including the last item).  If you leave off both indexes from
the slice, you get back the entire contents of the list.  So that's
what mylist[:] means.

Then you need to know that you can assign to the slice and it will
replace the old elements from the slice with the new ones.  You can
see that defined here, in the docs (second item in the table under
"4.6.3. Mutable Sequence Types"):

https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types

So, when you so mylist[:] = [0,1] you're taking all of the contents of
the existing list, and replacing them with the contents of the list
[0,1].  That changes the existing list, it doesn't just assign a new
list to the name mylist.

-- 
Jerry

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


#75036

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 15:31 -0700
Message-ID<557fa1ab-a8d7-4ef7-bc6f-978f9cba5091@googlegroups.com>
In reply to#75019
On Tuesday, July 22, 2014 4:46:25 PM UTC-4, emile wrote:
> On 07/22/2014 01:35 PM, Peter Pearson wrote:
> def reassign(mylist):  # no reason to shadow the list builtin
>      mylist[:] = [0,1]
> 
> mylist = [1]
> reassign(mylist)
> mylist
> 
> Emile

I have a new question on the code. When I run it in a file on PythonWin, 'mylist'
does not echo anything on the screen. While I enter the command line by line,
'mylist' shows the result:

>>> mylist
[0, 1]


What mechanism is involved?

Thanks,

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


#75038

Fromemile <emile@fenx.com>
Date2014-07-22 15:40 -0700
Message-ID<mailman.12205.1406068877.18130.python-list@python.org>
In reply to#75036
On 07/22/2014 03:31 PM, fl wrote:

> I have a new question on the code. When I run it in a file on PythonWin, 'mylist'
> does not echo anything on the screen. While I enter the command line by line,
> 'mylist' shows the result:
>
>>>> mylist
> [0, 1]
>
>
> What mechanism is involved?


As a convenience, the interactive prompt environment will display the 
value of a variable when entered on its own.

In scripts, you'd usually want to use print.

Emile


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


#75039

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 16:00 -0700
Message-ID<8b5a4a1f-656b-4871-91c4-2091209eafe3@googlegroups.com>
In reply to#75018
On Tuesday, July 22, 2014 4:35:33 PM UTC-4, Peter Pearson wrote:
> On Tue, 22 Jul 2014 12:34:51 -0700 (PDT), fl <r@gmail.com> wrote:
> When you say "def reassign(list)", that means "I'm defining a function
> to which the caller will pass one object, and within this function I'm
> going to refer to that object by the name 'list'."
> 
> 
> 
> Then, when you say "list=[0,1]", that means "Create the object [0,1],

The above is what rebind? see below I cite.

> and assign to it the name 'list'."  At this point, there is no longer
> any name that refers to the object that the caller passed.

Here is I find on-line about "Arguments are passed by assignment."
http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference

"If you pass a mutable object into a method, the method gets a reference to that 
same object and you can mutate it to your heart's delight, but if you rebind the
reference in the method, the outer scope will know nothing about it, and after
you're done, the outer reference will still point at the original object."

Thanks

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


#75041

Fromemile <emile@fenx.com>
Date2014-07-22 16:10 -0700
Message-ID<mailman.12207.1406070670.18130.python-list@python.org>
In reply to#75039
On 07/22/2014 04:00 PM, fl wrote:
> On Tuesday, July 22, 2014 4:35:33 PM UTC-4, Peter Pearson wrote:
>> On Tue, 22 Jul 2014 12:34:51 -0700 (PDT), fl <r@gmail.com> wrote:
>> When you say "def reassign(list)", that means "I'm defining a function
>> to which the caller will pass one object, and within this function I'm
>> going to refer to that object by the name 'list'."
>>
>>
>>
>> Then, when you say "list=[0,1]", that means "Create the object [0,1],
>
> The above is what rebind? see below I cite.

exactly.  assigning to a variable within a function makes that variable 
local to the function; assigning to the contents (as with [:]) changes 
the contents, but not the container variable, and as the container 
element was passed in you'll see the changed item outside the function.

Emile



>
>> and assign to it the name 'list'."  At this point, there is no longer
>> any name that refers to the object that the caller passed.
>
> Here is I find on-line about "Arguments are passed by assignment."
> http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference
>
> "If you pass a mutable object into a method, the method gets a reference to that
> same object and you can mutate it to your heart's delight, but if you rebind the
> reference in the method, the outer scope will know nothing about it, and after
> you're done, the outer reference will still point at the original object."
>
> Thanks
>

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


#75042

FromTerry Reedy <tjreedy@udel.edu>
Date2014-07-22 20:27 -0400
Message-ID<mailman.12208.1406075253.18130.python-list@python.org>
In reply to#75013
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'.

-- 
Terry Jan Reedy

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


#75046

Fromfl <rxjwg98@gmail.com>
Date2014-07-22 18:49 -0700
Message-ID<9219a041-0d6f-4e90-b209-5fd7c50ae1fd@googlegroups.com>
In reply to#75042
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. 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'.
> -- 
> Terry Jan Reedy

Thanks, but I don't understand your point yet. Could you give me another example
in which something is passed?




BTW, to a previous reply post. I have learned ':' in regular expression. But I am
still new to Python, I did not realize that it is the same ':' in the string
search/match.

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


#75048

FromBen Finney <ben+python@benfinney.id.au>
Date2014-07-23 11:59 +1000
Message-ID<mailman.12210.1406080801.18130.python-list@python.org>
In reply to#75046
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'.
>
> 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.

When you have a function ‘foo’ defined to expect a parameter, and you
specify an object (say, the object you have access to by the reference
‘bar’)::

    foo(bar)

What happens is that *the very same object* you're referring to by the
name ‘bar’ is then referenced by a *different* name inside the function
‘foo’. There is no passing; the same object gets a new local name
assigned to it, for use only within that function's code.

Function parameters aren't passed anywhere, they don't go anywhere, they
don't get cast or copied or anything else to the function. The function
gets to refer to the identical object by a name local in that function;
you can see what that parameter's name is by the definition of the
function.

-- 
 \       “Pray, v. To ask that the laws of the universe be annulled in |
  `\     behalf of a single petitioner confessedly unworthy.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney

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


#75055

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-23 05:35 +0000
Message-ID<53cf499c$0$29897$c3e8da3$5496439d@news.astraweb.com>
In reply to#75048
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'.
>>
>> 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."

They both describe what is being done, only from slightly different 
points of view. In mathematics, to call a function is a completely 
abstract action. Magic happens, and a result is returned. But in 
programming languages, calling a function has concrete actions: certain 
things have to happen even before the function itself executes. What sort 
of things? Well, for starters, somehow the arguments need to be passed to 
the function, so that the function can tell the difference between being 
called with x as argument and being called with y as argument.

If you say "nothing is being passed", then my response would be "Oh, you 
aren't calling the function at all? Or just calling it with no arguments?"

I maintain that treating "call" and "pass" as more-or-less the same thing 
is common terminology, used throughout both mathematics and computing, 
and there's no very little benefit to avoiding it in the case of Python.



-- 
Steven

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


#75059

FromChris Angelico <rosuav@gmail.com>
Date2014-07-23 16:07 +1000
Message-ID<mailman.12218.1406095649.18130.python-list@python.org>
In reply to#75055
On Wed, Jul 23, 2014 at 3:35 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> If you say "nothing is being passed", then my response would be "Oh, you
> aren't calling the function at all? Or just calling it with no arguments?"

The latter. Suppose you have a class method that takes optional args,
and you override it in a subclass. The subclass's method may choose to
swallow any args it was given, and "pass nothing" to the super()
method. I have code doing exactly this, and sometimes it's important
to comment it as such (although I'll usually word it as "pass on no
args" or something, rather than just "pass nothing"). But yes. If
there are any arguments, they are being passed.

ChrisA

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


#75061

FromBen Finney <ben+python@benfinney.id.au>
Date2014-07-23 16:25 +1000
Message-ID<mailman.12220.1406096728.18130.python-list@python.org>
In reply to#75055
Steven D'Aprano <steve@pearwood.info> writes:

> 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:
> >> > Nothing is being 'passed'.
> >>
> >> 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."
>
> They both describe what is being done, only from slightly different 
> points of view.

You're free to think that, but I'm agreeing with Terry that the
conflation is unwarranted and confusing in the attempt to explain what
is happening.

Rather, “Call the function ‘foo’ with ‘x’ as an argument” has no
implication that the argument ‘x’ travels anywhere, or is exclusively
located either inside the function or out of it, or any other inferences
that are invited by the statement “Pass ‘x’ to the function”.

So you may *intend* them to mean the same thing. But the terminology
comes with baggage, both from other programming languages and from
non-programming meanings of the English word “pass”. They don't
communicate the same thing.

> In mathematics, to call a function is a completely abstract action.

Right, and the terms aren't located anywhere we need to identify, they
don't travel anywhere, and “pass” isn't used to refer to them. These are
good reasons for avoiding the statement “pass a value ‘x’ to the
function ‘foo’”.

> But in programming languages, calling a function has concrete actions:
> certain things have to happen even before the function itself
> executes. What sort of things? Well, for starters, somehow the
> arguments need to be passed to the function

No. The function needs to *know what the arguments are*. Using the verb
“pass” to refer to an action which has nothing to do with motion through
any space is clearly confusing the matter here.

> If you say "nothing is being passed", then my response would be "Oh,
> you aren't calling the function at all? Or just calling it with no
> arguments?"

To which my response is “You're mistakenly conflating the above two
statements”.


Steven D'Aprano <steve@pearwood.info> writes:

> 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. […] 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!"

Because the objects 1 and 'x' are made known to and made available to
the function. They're not passed because they don't go anywhere.

I acknowledge that “pass the value 1 to the function ‘foo’” is
entrenched and I'm not advocating to remove it, but it's foolish to
ignore that this term invites unwarranted inferences.

Heck, you have argued strongly that the whole “how are values passed in
Python?” question is rather misguided and can only be answered by
casting out assumptions about “pass”. I'm saying the mis-guidance comes
from using the verb “pass a value” for an action that doesn't involve
values going anywhere.

-- 
 \      “It is the integrity of each individual human that is in final |
  `\        examination. On personal integrity hangs humanity's fate.” |
_o__)               —Richard Buckminster Fuller, _Critical Path_, 1981 |
Ben Finney

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web