Python function returns:

Tim Chase python.list at tim.thechases.com
Thu May 4 14:45:13 CEST 2006


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

This *is* the "better" and "standard" way to do it.  There 
are *worse* ways to emulate C/C++ if you want, but it takes 
being *more* obtruse.  Because certain objects are mutable, 
nothing[*] prevents you from doing something like

x = []
def foo(q):
	x.append(42)
foo(x)
print repr(x)

which will return that you've added "42" to your list. 
However, it's ugly and relies on side effects.

They Python way (that you deride) is much clearer.  Your 
input goes in the parameters, and your output gets assigned 
the way functions are intended to work.  Unambiguous.

I don't expect to call a sine function, and get the result 
in the parameter; rather I expect to get it as the result of 
the function.  Okay...unless I'm working in Assembly 
language (but that's one of many reasons *why* I choose 
Python ;)

Just a few thoughts,

-tkc
[*] nothing other than being given dirty looks by folks 
reading your code...









More information about the Python-list mailing list