468,134 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

return multiple values from fuction

hi, if someone can help me I would be grateful

when I do

def function
kjklj
llklÁ

return variableA, variableB

how can I assign the two return values to two distinct variables, as for ex.

varC = variableA
varD = variableB

??
Jul 18 '05 #1
6 43245
On Thu, 06 Nov 2003 19:58:55 +0000, Lupe <lu***@iname.com> wrote:
hi, if someone can help me I would be grateful

when I do

def function
kjklj
llklÁ

return variableA, variableB

how can I assign the two return values to two distinct variables, as for ex.

varC = variableA
varD = variableB

??

def test ():
...
return variableA, variableN
varC, vardD = test()

varC will contain variableA
varD ill contain variableB
Jul 18 '05 #2
Lupe wrote:
how can I assign the two return values to two distinct variables, as for ex.


By using tuple unpacking:

(varC, varD) = function()
--Irmen

Jul 18 '05 #3
Lupe wrote:
hi, if someone can help me I would be grateful

when I do

def function
this needs of course to be

def function():
kjklj
llklÁ

return variableA, variableB

how can I assign the two return values to two distinct variables, as for
ex.

varC = variableA
varD = variableB


"just do it":

varC, varD = function()
Alex

Jul 18 '05 #4
Alex Martelli wrote:
how can I assign the two return values to two distinct variables, as for
ex.

varC = variableA
varD = variableB

"just do it":

varC, varD = function()


I like that comment... "just do it"...
I find this is also true for most other things
that you want to do in Python.

"How do I create a mapping between a person's last
name and the list of telephone numbers he/she can be
reached at?" -- "umm.. just do it?"

{ "de Jong": ['234234', '34562363'] }

or whatever ;-)

--Irmen

Jul 18 '05 #5
I'm starting with Python and I find it really great!

It's... natural!

Lupe

Jul 18 '05 #6
Lupe wrote:
hi, if someone can help me I would be grateful

when I do

def function
kjklj
llklÁ

return variableA, variableB

how can I assign the two return values to two distinct variables, as for ex.

varC = variableA
varD = variableB

def a(): .... return "value 1", "value 2"
.... c, d = a()
c 'value 1' d 'value 2' e = a()
e

('value 1', 'value 2')

HTH

Jay
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by turtle | last post: by
4 posts views Thread by Aaron | last post: by
16 posts views Thread by Nikolay Petrov | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.