style query: function attributes for return codes?

george young gry at
Fri Dec 10 16:45:47 CET 2004

[python 2.3.3, x86 linux]
I recently found myself writing something like:

def get_connection():
    if tcp_conn():
        if server_allows_conn():
            return 'good_conn'
            return 'bad_auth'
        return 'no_server'

cn = get_connection()
if cn == 'good_con': ...

This is obviously just evil, since a misspelling in the string
return is treacherous.  I'm considering function attributes:

def get_connection():
    if tcp_conn():
        if server_allows_conn():
            return get_connection.GOOD
            return get_connection.BAD_AUTH
        return get_connection.NO_SERVER
get_connection.GOOD = 1
get_connection.BAD_AUTH = 2
get_connection.NO_SERVER = 3

If I put this function in it's own module, the solution is obvious:

def get_connection():
        return GOOD_CONN   

But if this is a small utility function that belongs inside a
larger module/class, I would like to have it's return values
closely associated with the function, not just another value in
the parent class.

Is anybody using function attributes like this?
Is this good python style?

-- George Young
"Are the gods not just?"  "Oh no, child.
What would become of us if they were?" (CSL)

More information about the Python-list mailing list