Quantcast

Can Python function return multiple data?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
85 messages Options
12345
fl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

fl
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,

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Joel Goldstick-2
On Tue, Jun 2, 2015 at 5:27 PM, fl <rxjwg98 at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

sohcahtoa82@gmail.com
In reply to this post by fl
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

John Gordon-2
In reply to this post by fl
In <3bbe49da-e989-4a8c-a8a9-75d3a786f508 at googlegroups.com> fl <rxjwg98 at 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 at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Chris Angelico
In reply to this post by fl
On Wed, Jun 3, 2015 at 7:27 AM, fl <rxjwg98 at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Thomas Rachel-3
In reply to this post by fl
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Chris Angelico
On Wed, Jun 3, 2015 at 11:56 PM, Thomas Rachel
<nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Mark Lawrence
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 at 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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

sohcahtoa82@gmail.com
In reply to this post by Chris Angelico
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 at 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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Chris Angelico
On Thu, Jun 4, 2015 at 11:30 AM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:

> On Wed, 3 Jun 2015 15:28:56 -0700 (PDT), sohcahtoa82 at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

random832@fastmail.us
In reply to this post by Mark Lawrence
On Wed, Jun 3, 2015, at 17:56, 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.

Python's in that same awkward space as Java, where it technically passes
by value, but the values are usually pointers, so passing a mutable
object (such as a list) allows the function to modify the caller's state
in a way that many people associate exclusively with by-reference.

Have I hit all the high points?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Serhiy Storchaka-2
In reply to this post by Chris Angelico
On 03.06.15 02:56, Chris Angelico wrote:

> On Wed, Jun 3, 2015 at 7:27 AM, fl <rxjwg98 at 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 :)

Closer modeling of C's model in python is to pass an object or a list as
an argument and set their attributes or elements in the function.

def f(a, b):
     a.x = 4.3
     a.y = 1.7
     b[:] = [45, 67]
     return 3



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Steven D'Aprano-8
In reply to this post by sohcahtoa82@gmail.com
On Thu, 4 Jun 2015 08:28 am, sohcahtoa82 at 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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Michael Torrie
In reply to this post by sohcahtoa82@gmail.com
On 06/03/2015 04:28 PM, sohcahtoa82 at gmail.com wrote:

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

Sure but if you reassign the variable that was passed it, it has no
effect whatsoever on the caller's variable, mutable or not.  This is why
people argue for "pass by value."  Because of this confusion, we often
say, "pass by object."

For example

def foo(bar):
    bar.append(5)
    bar = 6 #bar is no longer referring to a list

a=[1,2,3,4]
foo(a)
print (a)
# prints [1,2,3,4,5]







Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Marko Rauhamaa
In reply to this post by Steven D'Aprano-8
Steven D'Aprano <steve at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Grant Edwards-7
On 2015-06-04, Marko Rauhamaa <marko at pacujo.net> wrote:

> Steven D'Aprano <steve at 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            

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Marko Rauhamaa
Grant Edwards <invalid at 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

ElChino
In reply to this post by Steven D'Aprano-8
Steven D'Aprano wrote:

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

To quote Niklaus Wirth (the father of Pascal, Modula-2 etc.),
"You may call me by name. But you may also call me by reference".



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

Steven D'Aprano-8
In reply to this post by Grant Edwards-7
On Fri, 5 Jun 2015 12:37 am, Grant Edwards wrote:

> On 2015-06-04, Marko Rauhamaa <marko at 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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can Python function return multiple data?

random832@fastmail.us
In reply to this post by Steven D'Aprano-8
On Thu, Jun 4, 2015, at 09:47, Steven D'Aprano wrote:
> 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.

Well, no, because, in Java, if the type of x is int, then the value
really is 23. If it's Integer, then it's a reference to a boxed Integer
object. Which is (thankfully) immutable. But isn't invisible, unknown,
or unknowable at all.

Of course, in CPython, the type of an object reference is PyObject *.
Which isn't invisible, unknown, or unknowable, either.

If the value really were 23, the "is vs ==" problem wouldn't exist.

x = 500
y = x+1
z = y-1

x and z (probably) point to two different objects.

12345
Loading...