Returning variable results from a function

Delaney, Timothy tdelaney at avaya.com
Mon Apr 29 03:58:19 EDT 2002


> From: Matthew [mailto:matthewm at zebrasoft.co.nz]
> 
> Given something like:-
> 
> def fred(size):
>     return size, 1,2,3
> 
> size,a,b,c=fred(3)
> 
> how does one create a tuple to hold the results like:-
> 
> size, (<a tuple holding the number of results?>) = fred(<no of
> results?>)

What you want to do is return the 1, 2, 3 as a tuple ...

def spam (size):
    return size, (1, 2, 3,)

size, eggs = spam(3)

Here you are returning a tuple, which has size and another tuple as its
elements.

However, if you mean "how do I take 'size' and produce a tuple of that
length, already populated, and return it"

def spam (size):
    return size, tuple(range(1, size + 1))

size, eggs = spam(3)

Tim Delaney





More information about the Python-list mailing list