Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91898 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-06-02 14:27 -0700 |
| Last post | 2015-06-07 15:33 +1000 |
| Articles | 20 on this page of 84 — 22 participants |
Back to article view | Back to comp.lang.python
Can Python function return multiple data? fl <rxjwg98@gmail.com> - 2015-06-02 14:27 -0700
Re: Can Python function return multiple data? Joel Goldstick <joel.goldstick@gmail.com> - 2015-06-02 17:35 -0400
Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-02 14:40 -0700
Re: Can Python function return multiple data? John Gordon <gordon@panix.com> - 2015-06-02 21:40 +0000
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-03 09:56 +1000
Re: Can Python function return multiple data? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-06-03 15:56 +0200
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-04 07:35 +1000
Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-03 22:56 +0100
Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-03 15:28 -0700
Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-03 21:30 -0400
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-04 11:52 +1000
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-04 23:47 +1000
Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 17:25 +0300
Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-04 14:37 +0000
Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 18:04 +0300
Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-04 19:51 -0400
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 03:11 +1000
Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 20:30 +0300
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:37 +1000
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:30 -0400
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:31 +1000
Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-04 18:38 +0000
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:52 +1000
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-05 09:04 +1000
Re: Can Python function return multiple data? Grant Edwards <invalid@invalid.invalid> - 2015-06-05 02:02 +0000
Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 09:11 +0200
Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-05 12:27 +0300
Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 14:04 +0200
Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-04 21:52 +0100
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 09:13 +1000
Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 01:16 +0100
Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-05 02:40 +0100
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 11:48 +1000
Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 11:06 +0100
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-05 07:44 +1000
Re: Can Python function return multiple data? BartC <bc@freeuk.com> - 2015-06-05 10:51 +0100
Re: Can Python function return multiple data? ElChino <elchino@cnn.cn> - 2015-06-04 17:37 +0200
Re: Can Python function return multiple data? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-04 19:57 -0400
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:26 -0400
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:16 +1000
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 18:59 -0400
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 12:37 +1000
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-04 20:16 -0700
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 21:06 +1000
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 06:29 -0700
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 07:59 +1000
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 20:20 -0700
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-05 23:28 -0400
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 22:28 -0700
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 15:43 +1000
Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-07 08:49 -0700
Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:07 +1000
Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-07 09:20 -0700
Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:34 +1000
Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-20 18:59 -0700
Re: Lawful != Mutable (was Can Python function return multiple data?) Chris Angelico <rosuav@gmail.com> - 2015-06-21 12:32 +1000
Re: Lawful != Mutable (was Can Python function return multiple data?) Rustom Mody <rustompmody@gmail.com> - 2015-06-20 19:50 -0700
Re: Lawful != Mutable (was Can Python function return multiple data?) Laura Creighton <lac@openend.se> - 2015-06-21 11:14 +0200
Re: Lawful != Mutable (was Can Python function return multiple data?) Ron Adam <ron3200@gmail.com> - 2015-06-21 08:55 -0400
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:51 +1000
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 13:49 +1000
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-06 14:50 +1000
Re: Can Python function return multiple data? Chris Angelico <rosuav@gmail.com> - 2015-06-06 15:29 +1000
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-05 22:32 -0700
Re: Can Python function return multiple data? Dave Farrance <df@see.replyto.invalid> - 2015-06-06 07:52 +0100
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:53 +1000
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-05 08:21 -0400
Re: Can Python function return multiple data? Marko Rauhamaa <marko@pacujo.net> - 2015-06-05 16:37 +0300
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-04 19:07 -0700
Re: Can Python function return multiple data? Michael Torrie <torriem@gmail.com> - 2015-06-04 11:36 -0600
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 13:51 -0400
Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-04 20:17 +0200
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 08:45 +1000
Re: Can Python function return multiple data? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-05 08:59 +0200
Re: Can Python function return multiple data? Michael Torrie <torriem@gmail.com> - 2015-06-04 08:16 -0600
Re: Can Python function return multiple data? sohcahtoa82@gmail.com - 2015-06-05 11:55 -0700
Re: Can Python function return multiple data? random832@fastmail.us - 2015-06-04 01:01 -0400
Re: Can Python function return multiple data? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-04 12:34 -0600
Re: Can Python function return multiple data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-04 20:16 +0100
Re: Can Python function return multiple data? Serhiy Storchaka <storchaka@gmail.com> - 2015-06-04 09:56 +0300
Re: Can Python function return multiple data? fl <rxjwg98@gmail.com> - 2015-06-06 10:57 -0700
Re: Can Python function return multiple data? Rustom Mody <rustompmody@gmail.com> - 2015-06-06 21:35 -0700
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:47 +1000
Re: Can Python function return multiple data? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 15:33 +1000
Page 1 of 5 [1] 2 3 4 5 Next page →
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-06-02 14:27 -0700 |
| Subject | Can Python function return multiple data? |
| Message-ID | <3bbe49da-e989-4a8c-a8a9-75d3a786f508@googlegroups.com> |
Hi, I just see the tutorial says Python can return value in function, it does not say multiple data results return situation. In C, it is possible. How about Python on a multiple data return requirement? Thanks,
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-06-02 17:35 -0400 |
| Message-ID | <mailman.81.1433280959.13271.python-list@python.org> |
| In reply to | #91898 |
On Tue, Jun 2, 2015 at 5:27 PM, fl <rxjwg98@gmail.com> wrote:
> Hi,
>
> I just see the tutorial says Python can return value in function, it does
> not say multiple data results return situation. In C, it is possible.
> How about Python on a multiple data return requirement?
>
>
> Thanks,
> --
> https://mail.python.org/mailman/listinfo/python-list
Yes!
def my_function(n):
return n, n*2
a_number, its_double = my_function(3)
a_number will be 3, its_double will be 6
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2015-06-02 14:40 -0700 |
| Message-ID | <31ce682a-b4ab-46da-a9e4-ef7fc86d28c6@googlegroups.com> |
| In reply to | #91898 |
On Tuesday, June 2, 2015 at 2:27:37 PM UTC-7, fl wrote:
> Hi,
>
> I just see the tutorial says Python can return value in function, it does
> not say multiple data results return situation. In C, it is possible.
> How about Python on a multiple data return requirement?
>
>
> Thanks,
You return a tuple, set, or other iterable. For example:
def return_two_values():
return 1, 2
a, b = return_two_values()
print a
print b
This would print:
1
2
Note though that when doing something like this, you have to be really careful that if you have multiple calls to `return` in your function, that they will ALL return the same number of values. Otherwise, when the tuple/list/etc. is unpacked, you'll get an error.
def return_two_values():
# ... do some stuff
if someCondition:
print "someCondition was true!"
return 0
return 1, 2
a, b = return_two_values()
Here, if someCondition ended up being False, then an exception would be thrown.
Keep in mind that the unpacking of the returned value into the variables `a` and `b` works with *ANY* iterable. So if you returned 'abc' and unpacked it into three variables, then the first would contain 'a', the second 'b', and the third 'c'.
You can also just return a dictionary if you want to return multiple values.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-06-02 21:40 +0000 |
| Message-ID | <mkl7se$dsi$1@reader1.panix.com> |
| In reply to | #91898 |
In <3bbe49da-e989-4a8c-a8a9-75d3a786f508@googlegroups.com> fl <rxjwg98@gmail.com> writes:
> Hi,
> I just see the tutorial says Python can return value in function, it does
> not say multiple data results return situation. In C, it is possible.
> How about Python on a multiple data return requirement?
Python functions can return lists, tuples, dictionaries, or any other
container-like object.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-06-03 09:56 +1000 |
| Message-ID | <mailman.85.1433289398.13271.python-list@python.org> |
| In reply to | #91898 |
On Wed, Jun 3, 2015 at 7:27 AM, fl <rxjwg98@gmail.com> wrote: > I just see the tutorial says Python can return value in function, it does > not say multiple data results return situation. In C, it is possible. > How about Python on a multiple data return requirement? Technically, neither C nor Python can return multiple values from a single function call. In Python, the most common way to do this is to return a tuple, which can then be unpacked; as other posts in this thread have shown, this can look a lot like returning multiple values, and it's pretty convenient. In C, the nearest equivalent is passing a number of pointers as parameters, and having the function fill out values. Python's model is a lot closer to what you're saying than C's model is :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2015-06-03 15:56 +0200 |
| Message-ID | <mkn139$6hb$1@r01.glglgl.de> |
| In reply to | #91913 |
Am 03.06.2015 um 01:56 schrieb Chris Angelico: > and it's pretty convenient. In C, the nearest equivalent is passing a > number of pointers as parameters, and having the function fill out > values. Python's model is a lot closer to what you're saying than C's > model is :) At least, C functions can return structs... Thomas
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-06-04 07:35 +1000 |
| Message-ID | <mailman.129.1433367336.13271.python-list@python.org> |
| In reply to | #91966 |
On Wed, Jun 3, 2015 at 11:56 PM, Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> wrote: > Am 03.06.2015 um 01:56 schrieb Chris Angelico: > >> and it's pretty convenient. In C, the nearest equivalent is passing a >> number of pointers as parameters, and having the function fill out >> values. Python's model is a lot closer to what you're saying than C's >> model is :) > > > At least, C functions can return structs... Oh, yes, I forgot about that. Thought that was C++ but not C, partly because I never do it in either language. Although in a sense, a struct is still a single "thing". ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-06-03 22:56 +0100 |
| Message-ID | <mailman.133.1433368610.13271.python-list@python.org> |
| In reply to | #91966 |
On 03/06/2015 22:35, Chris Angelico wrote: > On Wed, Jun 3, 2015 at 11:56 PM, Thomas Rachel > <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> > wrote: >> Am 03.06.2015 um 01:56 schrieb Chris Angelico: >> >>> and it's pretty convenient. In C, the nearest equivalent is passing a >>> number of pointers as parameters, and having the function fill out >>> values. Python's model is a lot closer to what you're saying than C's >>> model is :) >> >> >> At least, C functions can return structs... > > Oh, yes, I forgot about that. Thought that was C++ but not C, partly > because I never do it in either language. Although in a sense, a > struct is still a single "thing". > > ChrisA > Don't forget that C functions can accept structs as input. Possibly not a good idea as I found out many years ago pre ANSIC when I forgot that little old ampersand, so the compiler didn't pick it up, but then with modern computers having so much memory who really cares if you burn a little bit of stack on structures rather than pointers to structures? Now does Python pass by value or by reference? Happily sits back and waits for 10**6 emails to arrive as this is discussed for the 10**6th time. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2015-06-03 15:28 -0700 |
| Message-ID | <e43a00ed-2b7d-497c-aa95-e30cbc04c04a@googlegroups.com> |
| In reply to | #92004 |
On Wednesday, June 3, 2015 at 2:57:00 PM UTC-7, Mark Lawrence wrote: > On 03/06/2015 22:35, Chris Angelico wrote: > > On Wed, Jun 3, 2015 at 11:56 PM, Thomas Rachel > > <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> > > wrote: > >> Am 03.06.2015 um 01:56 schrieb Chris Angelico: > >> > >>> and it's pretty convenient. In C, the nearest equivalent is passing a > >>> number of pointers as parameters, and having the function fill out > >>> values. Python's model is a lot closer to what you're saying than C's > >>> model is :) > >> > >> > >> At least, C functions can return structs... > > > > Oh, yes, I forgot about that. Thought that was C++ but not C, partly > > because I never do it in either language. Although in a sense, a > > struct is still a single "thing". > > > > ChrisA > > > > Don't forget that C functions can accept structs as input. Possibly not > a good idea as I found out many years ago pre ANSIC when I forgot that > little old ampersand, so the compiler didn't pick it up, but then with > modern computers having so much memory who really cares if you burn a > little bit of stack on structures rather than pointers to structures? > > Now does Python pass by value or by reference? Happily sits back and > waits for 10**6 emails to arrive as this is discussed for the 10**6th time. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence People actually argue that Python passes by value? This is easily proven wrong by passing a mutable object to a function and changing it within the function.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-06-03 21:30 -0400 |
| Message-ID | <mailman.140.1433381434.13271.python-list@python.org> |
| In reply to | #92008 |
On Wed, 3 Jun 2015 15:28:56 -0700 (PDT), sohcahtoa82@gmail.com declaimed
the following:
>
>People actually argue that Python passes by value? This is easily proven wrong by passing a mutable object to a function and changing it within the function.
Which only proves you passed a mutable object... Mutating the object
means you've performed some operation that "goes inside the box".
Try assigning a new object to it rather than mutating it... IE; don't
go inside the box, but try to change the box itself. Doesn't carry out --
which is the traditional definition of pass-by-reference: assignment
changes the passed-in-object.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-06-04 11:52 +1000 |
| Message-ID | <mailman.141.1433382739.13271.python-list@python.org> |
| In reply to | #92008 |
On Thu, Jun 4, 2015 at 11:30 AM, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote: > On Wed, 3 Jun 2015 15:28:56 -0700 (PDT), sohcahtoa82@gmail.com declaimed > the following: > >> >>People actually argue that Python passes by value? This is easily proven wrong by passing a mutable object to a function and changing it within the function. > > Which only proves you passed a mutable object... Mutating the object > means you've performed some operation that "goes inside the box". > > Try assigning a new object to it rather than mutating it... IE; don't > go inside the box, but try to change the box itself. Doesn't carry out -- > which is the traditional definition of pass-by-reference: assignment > changes the passed-in-object. So you've proven that Python doesn't pass by value, and that Python doesn't pass by reference. I'm still waiting for "pass-by-quantum-entanglement" to be a viable option. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-04 23:47 +1000 |
| Message-ID | <557056f9$0$13009$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #92008 |
On Thu, 4 Jun 2015 08:28 am, sohcahtoa82@gmail.com wrote:
> On Wednesday, June 3, 2015 at 2:57:00 PM UTC-7, Mark Lawrence wrote:
[...]
>> Now does Python pass by value or by reference? Happily sits back and
>> waits for 10**6 emails to arrive as this is discussed for the 10**6th
>> time.
> People actually argue that Python passes by value? This is easily proven
> wrong by passing a mutable object to a function and changing it within the
> function.
You would be surprised how many people don't allow either facts or common
sense to get in the way of their preconceived notions.
The Python community has more or less reached consensus that Python uses the
calling convention usually called "pass by object" or "call by sharing"
(although it has a few other names), especially since the Python docs uses
the term.
But you still find a few people here and there who have been exposed to Java
foolishness, and will argue that Python is "pass by value, where the value
is an implementation dependent reference to the thing that you thought was
the value".
In other words, according to this Java philosophy, following `x = 23`, the
value of x is not 23 like any sane person would expect, but some invisible
and unknown, and unknowable, reference to 23.
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"
http://effbot.org/zone/call-by-object.htm
This may also be helpful:
http://import-that.dreamwidth.org/1130.html
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2015-06-04 17:25 +0300 |
| Message-ID | <87a8wf5z4l.fsf@elektro.pacujo.net> |
| In reply to | #92052 |
Steven D'Aprano <steve@pearwood.info>:
> But you still find a few people here and there who have been exposed
> to Java foolishness, and will argue that Python is "pass by value,
> where the value is an implementation dependent reference to the thing
> that you thought was the value".
Why fight terminology? Definitions can't be proved right or wrong.
Anyway, I would say Python definitely is in the classic pass-by-value
camp. Here's a simple test:
def f(x):
x = 3
y = 1
f(y)
print(y)
If it prints 1, it's pass by value. If it prints 3, it's pass by
reference.
Marko
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2015-06-04 14:37 +0000 |
| Message-ID | <mkpnqm$gfn$1@reader1.panix.com> |
| In reply to | #92060 |
On 2015-06-04, Marko Rauhamaa <marko@pacujo.net> wrote:
> Steven D'Aprano <steve@pearwood.info>:
>
>> But you still find a few people here and there who have been exposed
>> to Java foolishness, and will argue that Python is "pass by value,
>> where the value is an implementation dependent reference to the thing
>> that you thought was the value".
>
> Why fight terminology? Definitions can't be proved right or wrong.
>
> Anyway, I would say Python definitely is in the classic pass-by-value
> camp. Here's a simple test:
>
> def f(x):
> x = 3
>
> y = 1
> f(y)
> print(y)
>
> If it prints 1, it's pass by value. If it prints 3, it's pass by
> reference.
Somebody else might just as honestly say that it's pass by reference:
def f(x):
x[2] = 2;
x = ['a','b','c']
f(x)
print(x)
If it prints ['a','b','c'], it's pass by value. If it's pass by
reference, it prints ['a', 'b', 2].
IMO, it's pass by reference.
But, discussing pass-by-this vs. pass-by-that without also discussing
the semantics of the assignment operator is rather pointless. Not
that the pointlessness of an argument is going to slow down a
thread...
--
Grant Edwards grant.b.edwards Yow! I request a weekend in
at Havana with Phil Silvers!
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2015-06-04 18:04 +0300 |
| Message-ID | <874mmn5xbb.fsf@elektro.pacujo.net> |
| In reply to | #92062 |
Grant Edwards <invalid@invalid.invalid>:
>> Anyway, I would say Python definitely is in the classic pass-by-value
>> camp. Here's a simple test:
>>
>> def f(x):
>> x = 3
>>
>> y = 1
>> f(y)
>> print(y)
>>
>> If it prints 1, it's pass by value. If it prints 3, it's pass by
>> reference.
>
> Somebody else might just as honestly say that it's pass by reference:
Yes, but that would be a shift from the 1970's notion:
In pass-by-value, the actual parameter is evaluated. The value of the
actual parameter is then stored in a new location allocated for the
function parameter.
[...]
In pass-by-reference, the actual parameter must have an L-value. The
L-value of the actual parameter is then bound to the formal
parameter.
[...]
* A parameter in Pascal is normally passed by value. It is passed by
reference, however, if the keyword var appears before the
declaration of the formal parameter.
procedure proc(in: Integer; var out: Real);
* The only parameter-passing method in C is call-by-value; however,
the effect of call-by-reference can be achieved using pointers. In
C++ true call-by-reference is available using reference parameters.
<URL: http://www.cl.cam.ac.uk/teaching/0910/ConceptsPL/Algol-Pasca
l.pdf>
Pass by reference could easily be added to Python, too, if that were
deemed useful.
Marko
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-06-04 19:51 -0400 |
| Message-ID | <mailman.185.1433461888.13271.python-list@python.org> |
| In reply to | #92063 |
On Thu, 04 Jun 2015 18:04:56 +0300, Marko Rauhamaa <marko@pacujo.net>
declaimed the following:
>
>Yes, but that would be a shift from the 1970's notion:
>
> In pass-by-value, the actual parameter is evaluated. The value of the
> actual parameter is then stored in a new location allocated for the
> function parameter.
>
Except that this does NOT happen with common Python. Python doesn't
"store in a new location"...
>>> def func(parm):
... print id(parm), id(a), parm is a
... parm = 6
... print id(parm), id(a), parm is a
...
>>> a = 3
>>> func(a)
4145800 4145800 True
4145728 4145800 False
>>> print a, id(a)
3 4145800
>>>
In the first print, "parm" and "a" ARE THE SAME OBJECT
After the assignment to parm, "parm" and "a" are no longer the same
object.
In your "1970's notion", "parm" and "a" should already be different
object by the time of the first print statement.
In common Python, id() basically returns the address of the object. "a"
is a reference to an object with the value "3"... On entry to func(),
"parm" is aso a reference to the same object (same address/ID).
But after the assignment, "parm" is now a reference to a different
object.
Python does not expose the old "mailboxes" concept to the user... That
is: where the languages of the 50s-70s all tied variable names to fixed
addresses (boxes) in memory, and assignment copies the contents of one box
to another box (and this applies also to parameter passing), Python instead
has a pile of "objects", a bunch of names on "post-it notes" (with strings
attached to them), and assignment works by moving the string from one
object to another object -- the location of the name does not change, only
the other end of the string changes what object it names.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-05 03:11 +1000 |
| Message-ID | <557086d6$0$13011$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #92062 |
On Fri, 5 Jun 2015 12:37 am, Grant Edwards wrote:
> On 2015-06-04, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Anyway, I would say Python definitely is in the classic pass-by-value
>> camp. Here's a simple test:
>>
>> def f(x):
>> x = 3
>>
>> y = 1
>> f(y)
>> print(y)
>>
>> If it prints 1, it's pass by value. If it prints 3, it's pass by
>> reference.
Wrong. Why do you [Marko] imagine that pass-by-value and pass-by-reference
are the only two options? Your test is insufficient to distinguish pass by
value from pass by sharing, or pass by reference from pass by name.
You need at least one more test to prove pass by value: you need to
demonstrate that the value bound to y is copied when passed to the
function. E.g. pass a mutable value (say, a list) and mutate it inside the
function. If the list in the outer scope is *not* mutated, then and only
then can you say it is pass by value.
> Somebody else might just as honestly say that it's pass by reference:
And they would be just as wrong.
> def f(x):
> x[2] = 2;
>
> x = ['a','b','c']
> f(x)
> print(x)
>
> If it prints ['a','b','c'], it's pass by value. If it's pass by
> reference, it prints ['a', 'b', 2].
Wrong. Why do you [Grant] imagine that pass by value and pass by reference
are the only two options? Your test is insufficient to distinguish pass by
reference from pass by name or pass by sharing, all three of which will
give the same behaviour for this specific test.
You need at least two more tests to demonstrate that this is pass by
reference. First you need to demonstrate that function f can rebind the
variable in the outer scope. E.g. take Marko's test above, if it is pass by
reference or pass by name it will print 3. (Hint: it doesn't.) Then you
need to demonstrate that it isn't pass by name. Despite the name, "pass by
name" doesn't actually require a variable name, it can also accept an
expression or literal. Pass by reference cannot.
def f(x):
pass
f(23)
If that raises an exception, then it is pass by reference. (Hint: it
doesn't.)
> IMO, it's pass by reference.
It really isn't.
> But, discussing pass-by-this vs. pass-by-that without also discussing
> the semantics of the assignment operator is rather pointless.
No, that's a red-herring.
I don't know of any programming language where passing a value to a
function, and binding it to a name (or assigning it to a variable, if you
prefer) uses different mechanisms. If the language offers two or more
argument passing mechanisms, assignment uses one of them (e.g. Algol offers
pass by name and pass by value, and assignment is by value). If there is
any language where assignment uses one style and argument passing always
uses another, I've never come across it.
> Not
> that the pointlessness of an argument is going to slow down a
> thread...
Clear thinking is pointless?
Well, that explains how you can claim that Python is pass by reference
immediately after Marko demonstrates that it cannot possibly be pass by
reference *wink*
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2015-06-04 20:30 +0300 |
| Message-ID | <874mmn2xgl.fsf@elektro.pacujo.net> |
| In reply to | #92068 |
Steven D'Aprano <steve@pearwood.info>: >> On 2015-06-04, Marko Rauhamaa <marko@pacujo.net> wrote: >>> If it prints 1, it's pass by value. If it prints 3, it's pass by >>> reference. > > Wrong. Why do you [Marko] imagine that pass-by-value and > pass-by-reference are the only two options? It's a classic dichotomy. The example I provided illustrates the distinction between the two. Marko
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-05 08:37 +1000 |
| Message-ID | <5570d33a$0$12985$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #92074 |
On Fri, 5 Jun 2015 03:30 am, Marko Rauhamaa wrote: > Steven D'Aprano <steve@pearwood.info>: > >>> On 2015-06-04, Marko Rauhamaa <marko@pacujo.net> wrote: >>>> If it prints 1, it's pass by value. If it prints 3, it's pass by >>>> reference. >> >> Wrong. Why do you [Marko] imagine that pass-by-value and >> pass-by-reference are the only two options? > > It's a classic dichotomy. The example I provided illustrates the > distinction between the two. No it isn't, and no it doesn't. Call by name, call by [object] sharing, call by copy-restore, call by need, call by macro expansion... where do these appear in your false dichotomy? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2015-06-04 13:30 -0400 |
| Message-ID | <mailman.169.1433439055.13271.python-list@python.org> |
| In reply to | #92068 |
On Thu, Jun 4, 2015, at 13:11, Steven D'Aprano wrote:
> You need at least one more test to prove pass by value: you need to
> demonstrate that the value bound to y is copied when passed to the
> function. E.g. pass a mutable value (say, a list) and mutate it inside
> the
> function. If the list in the outer scope is *not* mutated, then and only
> then can you say it is pass by value.
That's not true at all. Being able to mutate the object doesn't make it
not-pass-by-value any more than being able to use an int argument as an
index to mutate a global array makes the int not-pass-by-value.
a = [0]*16
def f(x, i):
x[i] = 3
f(a, 0)
If x is not pass-by-value, then clearly i is also not pass-by-value,
despite integers being immutable. Being able to use an argument to cause
a side effect does not mean the argument has not been passed by value.
a hasn't changed, 0 hasn't changed, the only thing that has changed is
the list object, which is *not the same thing as a*.
[toc] | [prev] | [next] | [standalone]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.python
csiph-web