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


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

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

Started bydec135@msn.com
First post2013-12-22 14:54 -0800
Last post2013-12-23 01:58 -0800
Articles 4 — 4 participants

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


Contents

  How can i return more than one value from a function to more than one variable dec135@msn.com - 2013-12-22 14:54 -0800
    Re: How can i return more than one value from a function to more than one variable Simon Hayward <mail@simonsblog.co.uk> - 2013-12-23 09:36 +0000
    Re: How can i return more than one value from a function to more than one variable Rick Johnson <rantingrickjohnson@gmail.com> - 2013-12-22 16:10 -0800
    Re: How can i return more than one value from a function to more than one variable Gary Herron <gary.herron@islandtraining.com> - 2013-12-23 01:58 -0800

#62607 — How can i return more than one value from a function to more than one variable

Fromdec135@msn.com
Date2013-12-22 14:54 -0800
SubjectHow can i return more than one value from a function to more than one variable
Message-ID<40ddd2cf-483f-423b-9a4d-93f6a5e77df2@googlegroups.com>
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

[toc] | [next] | [standalone]


#62609

FromSimon Hayward <mail@simonsblog.co.uk>
Date2013-12-23 09:36 +0000
Message-ID<mailman.4543.1387792338.18130.python-list@python.org>
In reply to#62607
> 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

Using multiple assignment.

# Swap values
x, y = 4, 7
y, x = x, y

Or to keep this in a function, return a tuple and assign from that:

def switch(x, y):
    return y, x

x, y = switch(x, y)

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


#62612

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-12-22 16:10 -0800
Message-ID<2970f228-daf5-4c01-8f01-35949aaf440f@googlegroups.com>
In reply to#62607
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)

If your intent is to swap the values of two variables (which
is really rebinding the names only) then why do you need a
function at all when you could explicitly swap the values
yourself? What gain would a function give you -- even if you
could do it?

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


#62613

FromGary Herron <gary.herron@islandtraining.com>
Date2013-12-23 01:58 -0800
Message-ID<mailman.4545.1387793194.18130.python-list@python.org>
In reply to#62607
On 12/22/2013 02:54 PM, dec135@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

I don't' understand the question, but if you are just trying to exchange 
the values of x and y, this will do:

x,y = y,x


If you want a function to return several values to several variables, try:

def fn(...):
     # calculate a and b
     return a,b

p,q = fn(...)

All these comma-separated sequences are tuples, often written with 
parentheses as (x,y)=(y,x) and (p,q)=fn(...), but as here,  the 
parentheses are often not necessary.

Gary Herron

[toc] | [prev] | [standalone]


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


csiph-web