Recommendations (or best practices) to define functions (or methods)

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Jan 8 16:24:55 EST 2007


vizcayno a écrit :
> Hello:
> Need your help in the "correct" definition of the next function. If
> necessary, I would like to know about a web site or documentation that
> tells me about best practices in defining functions, especially for
> those that consider the error exceptions management.
> I have the next alternatives but I think there are better:
> 
> Alternative 1:
> =============
> def ExecuteSQL(cmdSQL, cursor):

The recommended naming scheme is all_lower for functions, methods and 
variables, CamelCase for classes and ALL_UPPER for pseudo-constants.

>     try:
>         cursor.execute(cmdSQL)
>     except Exception, e:
>         return e
>     return 1
> 
> Seems a good solution

Err... The whole point of exceptions is to avoid using return values as 
error code. Your code would be *much* better without this braindead 
(sorry) "exception handling":

def execute_sql(sql, cursor):
   cursor.execute(sql)

And now, since it's obvious that it's just a totally useless function 
call. So just ditch this function, and just call cursor.execute directly !-)

(snip other examples of how to not handle exceptions...)



More information about the Python-list mailing list