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


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

Re: How can i return more than one value from a function to more than one variable

Started byBob Rashkin <rrashkin@gmail.com>
First post2013-12-22 15:41 -0800
Last post2013-12-23 11:45 +1100
Articles 6 — 5 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How can i return more than one value from a function to more than one variable Bob Rashkin <rrashkin@gmail.com> - 2013-12-22 15:41 -0800
    Re: How can i return more than one value from a function to more than one variable Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-22 19:05 -0500
    Re: How can i return more than one value from a function to more than one variable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-23 00:37 +0000
      Re: How can i return more than one value from a function to more than one variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-22 22:17 -0500
    Re: How can i return more than one value from a function to more than
 one variable Dave Angel <davea@davea.name> - 2013-12-22 19:57 -0500
    Re: How can i return more than one value from a function to more than one variable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-23 11:45 +1100

#62562 — Re: How can i return more than one value from a function to more than one variable

FromBob Rashkin <rrashkin@gmail.com>
Date2013-12-22 15:41 -0800
SubjectRe: How can i return more than one value from a function to more than one variable
Message-ID<8e01f218-ec00-4d01-ab82-53e1ea7670e2@googlegroups.com>
On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:
> basically what I wanna do is this :
> 
> 
> 
> x = 4
> 
> y = 7
> 
> def switch (z,w):
> 
> ***this will switch z to w and vice verca***
> 
>      c= z
> 
>      z=w
> 
>      w=c
> 
>      print 'Now x =', w, 'and y = ' , z
> 
>      return w
> 
> x = switch(x,y)
> 
> 
> 
>  How am I supposed to do so I can  return also a value to the variable y WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?
> 
> 
> 
> thanks in advance

Not sure I understand the problem but I think the answer is to put multiple values in a list and return the list.

[toc] | [next] | [standalone]


#62563

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-12-22 19:05 -0500
Message-ID<mailman.4517.1387757150.18130.python-list@python.org>
In reply to#62562

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

return a tuple:

return a, b, c

or whatever


On Sun, Dec 22, 2013 at 6:41 PM, Bob Rashkin <rrashkin@gmail.com> wrote:

> On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:
> > basically what I wanna do is this :
> >
> >
> >
> > x = 4
> >
> > y = 7
> >
> > def switch (z,w):
> >
> > ***this will switch z to w and vice verca***
> >
> >      c= z
> >
> >      z=w
> >
> >      w=c
> >
> >      print 'Now x =', w, 'and y = ' , z
> >
> >      return w
> >
> > x = switch(x,y)
> >
> >
> >
> >  How am I supposed to do so I can  return also a value to the variable y
> WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?
> >
> >
> >
> > thanks in advance
>
> Not sure I understand the problem but I think the answer is to put
> multiple values in a list and return the list.
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

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


#62567

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-12-23 00:37 +0000
Message-ID<52b785c4$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#62562
Unfortunately, the original post seems to have gone missing here, so 
please excuse me for breaking threading.

On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:
> basically what I wanna do is this :
> 
> x = 4
> y = 7
> 
> def switch (z,w):
> ***this will switch z to w and vice verca***
>     c= z
>     z=w
>     w=c
>     print 'Now x =', w, 'and y = ' , z
>     return w
>
> x = switch(x,y)
> 
>
>  How am I supposed to do so I can  return also a value to the variable
>  y WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?


To swap two values in Python (or for that matter, three or thirty-three 
values!) just re-assign the values. Python guarantees that this will work:

x = 23
y = 42
x, y = y, x

x now has the value that y had, and y has the value that x had. There is 
no need for a temporary value, and no need for a "switch" function.

To return more than one value from a function, return a list or a tuple. 
Normally we use a tuple:


def sum_and_product(x, y):
    sum = x + y
    product = x*y
    return (sum, product)


a = 100
b = 2
s, p = sum_and_product(a, b)

Now s will have the value 102 and p will have the value 200.




-- 
Steven

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


#62581

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-12-22 22:17 -0500
Message-ID<mailman.4523.1387768660.18130.python-list@python.org>
In reply to#62567
On 23 Dec 2013 00:37:24 GMT, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> declaimed the following:

>Unfortunately, the original post seems to have gone missing here, so 
>please excuse me for breaking threading.
>
	It's not you... It didn't make it to me either, and search by reference
message ID (based on the reply by one who did see it) isn't finding the
message either.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


#62568 — Re: How can i return more than one value from a function to more than one variable

FromDave Angel <davea@davea.name>
Date2013-12-22 19:57 -0500
SubjectRe: How can i return more than one value from a function to more than one variable
Message-ID<mailman.4518.1387760206.18130.python-list@python.org>
In reply to#62562
On Sun, 22 Dec 2013 15:41:06 -0800 (PST), Bob Rashkin 
<rrashkin@gmail.com> wrote:
> On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:
> >  How am I supposed to do so I can  return also a value to the 
variable y WITHOUT printing 'Now x =', w, 'and y = ' , z   a second 
time ?

You are apparently asking 3 questions. 

To exchange 2 values, use the tuple-unpack approach.

a, b = b, a

To get the equivalent of multiple return values,  return a tuple.

def myfunc (a):
     x = a*a
     y = 2+a
     return x, y

p , q = myfunc (5)

To avoid executing your print twice, call the function only once.

And a bonus one: to avoid my getting a blank message in this text 
newsgroup,  post in text,  not html.

-- 
DaveA

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


#62570

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-12-23 11:45 +1100
Message-ID<52b787bb$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#62562
Something funny is going on here, not only has the original gone missing,
but my reply apparently has also gone missing. Let me try again, and
apologies for if you see duplicate messages.

On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:

> basically what I wanna do is this :
> 
> x = 4
> y = 7
> 
> def switch (z,w):
> ***this will switch z to w and vice verca***
>      c= z
>      z=w
>      w=c
>      print 'Now x =', w, 'and y = ' , z
>      return w
> 
> x = switch(x,y)
> 
>
>  How am I supposed to do so I can  return also a value to the variable y
>  WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?


There is no need for a function to swap two values. In Python, if you want
to swap two values (or three, or thirty-three for that matter!) just
reassign them all at once.

x = 23
y = 42
x, y = y, x


Python guarantees that after this, x will have the value that y had, and y
will have the value that x had. No need for a function!


To return two or more values from a function, use a tuple:


def sum_and_product(a, b):
    """Return the sum and product of a and b."""
    sum = a+b
    product = a*b
    return (sum, product)


s, p = sum_and_product(100, 2)


s will now have the value 102, and p the value 200.



-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web