Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return multiple values from fuction

0 views
Skip to first unread message

Lupe

unread,
Nov 6, 2003, 2:58:55 PM11/6/03
to
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

??


Jay O'Connor

unread,
Nov 6, 2003, 3:03:23 PM11/6/03
to

def test ():
...
return variableA, variableN

varC, vardD = test()

varC will contain variableA
varD ill contain variableB


Irmen de Jong

unread,
Nov 6, 2003, 3:14:15 PM11/6/03
to
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

Alex Martelli

unread,
Nov 6, 2003, 3:24:52 PM11/6/03
to
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

Irmen de Jong

unread,
Nov 6, 2003, 3:35:08 PM11/6/03
to
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

Lupe

unread,
Nov 6, 2003, 3:52:01 PM11/6/03
to
I'm starting with Python and I find it really great!

It's... natural!

Lupe

Jay Dorsey

unread,
Nov 6, 2003, 1:23:49 PM11/6/03
to pytho...@python.org

>>> 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


Batista, Facundo

unread,
Nov 6, 2003, 3:04:50 PM11/6/03
to Python-List (E-mail)
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 f():
return (1, 2)

>>> (a, b) = f()
>>> a
1
>>> b
2
>>>

. Facundo

Lupe

unread,
Nov 6, 2003, 3:52:01 PM11/6/03
to pytho...@python.org

I'm starting with Python and I find it really great!

It's... natural!

Lupe

0 new messages