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


Groups > comp.lang.python > #62613

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

Date 2013-12-23 01:58 -0800
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: How can i return more than one value from a function to more than one variable
References <40ddd2cf-483f-423b-9a4d-93f6a5e77df2@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4545.1387793194.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web