style query: function attributes for return codes?

Robert Kern rkern at ucsd.edu
Fri Dec 10 12:42:32 EST 2004


Steven Bethard wrote:

> Sorry, I also meant to add that the other obvious way of dealing with 
> this kind of thing is to make the results keyword parameters:
> 
> def get_connection(GOOD=1, BAD_AUTH=2, NO_SERVER=3):
>     if tcp_conn():
>         if server_allows_conn():
>             return GOOD
>         else:
>             return BAD_AUTH
>     else:
>         return NO_SERVER
> 
> This has the benefit that if your user wants different return values 
> they can specify them, but the disadvantage that someone improperly 
> calling the function with more than 0 parameters will get, instead of an 
> error message, a strange return value.

Another disadvantage is that one must compare the return value by value 
and not by name. That is, I cannot do something like this:

code = get_connection()
if code == NO_SERVER:
     ...

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list