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


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

Can Python function return multiple data?

Started byfl <rxjwg98@gmail.com>
First post2015-06-02 14:27 -0700
Last post2015-06-07 15:33 +1000
Articles 4 on this page of 84 — 22 participants

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


Contents

  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 5 of 5 — ← Prev page 1 2 3 4 [5]


#92185

Fromfl <rxjwg98@gmail.com>
Date2015-06-06 10:57 -0700
Message-ID<ccc734ce-f9c7-43b6-a00b-7349c214dd0e@googlegroups.com>
In reply to#91898
On Saturday, June 6, 2015 at 9:39:19 AM UTC-7, Amir Arsalan wrote:
> you can use yield structure in python for multiple return. ex:
> 
> 
> def func(a):
>     yield a*2
>     print "a*2"
>     yield a*3
>     print "a*3"
>     ...
> 
> 
> data = func(5) --> data = (10,15,... )
> 
> 
> On Wed, Jun 3, 2015 at 1:57 AM, fl <rxj...@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

Excuse me. I input the following according to your idea, but I do not 
understand how to use it from the echo. It does not show how to use
the multiple output results. I am a new Python user. Please give a little
more explanation if you could.


Thanks,

>>> def func(a):
	yield a*2
	print "a*2"
	yield a*3
	print "a*3"

>>> data=func(5)
>>> data
<generator object func at 0x02B97260>
>>> list(data)
a*2
a*3
[10, 15]

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


#92198

FromRustom Mody <rustompmody@gmail.com>
Date2015-06-06 21:35 -0700
Message-ID<e81b65c8-9615-4397-a66f-030a60db008e@googlegroups.com>
In reply to#92185
On Saturday, June 6, 2015 at 11:27:29 PM UTC+5:30, fl wrote:
> On Saturday, June 6, 2015 at 9:39:19 AM UTC-7, Amir Arsalan wrote:
> > you can use yield structure in python for multiple return. ex:
> > 
> > 
> > def func(a):
> >     yield a*2
> >     print "a*2"
> >     yield a*3
> >     print "a*3"
> >     ...
> > 
> > 
> > data = func(5) --> data = (10,15,... )
> > 
> > 
> > On Wed, Jun 3, 2015 at 1:57 AM, 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,
> > 
> > --
> > 
> > https://mail.python.org/mailman/listinfo/python-list
> 
> Excuse me. I input the following according to your idea, but I do not 
> understand how to use it from the echo. It does not show how to use
> the multiple output results. I am a new Python user. Please give a little
> more explanation if you could.

You still have this question?
Ok apologies for our long rambles far from your question.

But Joel Goldstick gave right at the start a neat and complete answer. I repeat 
it with the tuples made more explicit

def my_function(n):
    return (n, n*2)   # return multiple vals as tuple

# somewhere, sometime else
(a_number, its_double) = my_function(3)  # receive the tuple

Is there something about it that you dont understand or find unsatisfactory?

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


#92200

FromSteven D'Aprano <steve@pearwood.info>
Date2015-06-07 15:47 +1000
Message-ID<5573db09$0$12995$c3e8da3$5496439d@news.astraweb.com>
In reply to#92185
On Sun, 7 Jun 2015 03:57 am, fl wrote:

> Excuse me. I input the following according to your idea, but I do not
> understand how to use it from the echo. It does not show how to use
> the multiple output results. I am a new Python user. Please give a little
> more explanation if you could.
> 
> 
> Thanks,
> 
>>>> def func(a):
> yield a*2
> print "a*2"
> yield a*3
> print "a*3"


This is called a "generator function". It is a special type of function that
be stopped and restarted in the middle. Instead of "return", which exits
the function, you use "yield" which provides a result, then pauses the
function waiting to restart at the next line.

When you call x = func(5), it does NOT execute the body of the function.
Instead, it creates a "generator object", or just "generator". When you
display x, you see something like this:

<generator object func at 0x02B97260>

To run the body of func, we need to run x. There are a few different ways.
The usual way is to iterate over it:

for item in x:
    print "item:", item

This will print:

item: 10
a*2
item: 15
a*3

then stop. Do you understand why?

Once you have done this, the generator is exhausted. There are no more
values left, and so we're done with it.

Another way is to pass the generator to a function like list() or tuple().

Remember, because x is exhausted, you have to start with a new generator.
Just run x = func(5) again, and you will have a brand new fresh generator.
Now call list(x), the list function will iterate over the items yielded,
and assemble them into a list or tuple. "a*2" and "a*3" will be printed,
and the list will be [10, 15].


-- 
Steven

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


#92199

FromSteven D'Aprano <steve@pearwood.info>
Date2015-06-07 15:33 +1000
Message-ID<5573d79a$0$12983$c3e8da3$5496439d@news.astraweb.com>
In reply to#91898
Hi Amir, and welcome!

On Sun, 7 Jun 2015 02:38 am, Amir Arsalan wrote:

> you can use yield structure in python for multiple return. ex:
> 
> def func(a):
>     yield a*2
>     print "a*2"
>     yield a*3
>     print "a*3"
>     ...
> 
> data = func(5) --> data = (10,15,... )

That's actually wrong. If you run:

data = func(5)

data does NOT equal the tuple (10, 15, ...) but it is a generator object.
Iterating over the generator will yield 10, 15 etc one item at a time.


Please remember that the Original Poster is a beginner. He is having trouble
with *basic* Python questions, like returning multiple items from a single
function call. Showing him advanced techniques like generators, especially
when the example you show is wrong, will just confuse him more.



-- 
Steven

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

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


csiph-web