Python function returns:

Diez B. Roggisch deets at nospam.web.de
Thu May 4 14:35:25 CEST 2006


Michael Yanowitz wrote:

>   I am still new to Python but have used it for the last 2+ months.
> One thing I'm still not used to is that functions parameters can't
> change as expected. For example in C, I can have
>  status = get_network_info (strIpAddress, &strHostname, &nPortNumber)
>   where this fictitious function returns a status, but also returns
>   modified
> values for a hostname and a port number.
>   In Python, there does not seem to be an easy way to have functions
>   return
> multiple values except it can return a list such as:
> strHostname, nPortNumber, status = get_network_info (strIpAddress,
> strHostname,
>                                                      nPortNumber)
>   Am I missing something obvious? Is there a better, or more standard way
> to return values from functions?

No, that exactly is the way to go. But usually one uses tuples and the
possibility of sequence-unpacking together to reach a solution tha at least
to my eye looks more favorable:


def foo(a, b):
    return a*b, a+c

a = 10
b = 20

a, b = foo(a, b)


Diez



More information about the Python-list mailing list