Coding style and else statements

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Aug 28 18:30:37 EDT 2006


tobiah a écrit :
> def foo(thing):
> 
>     if thing:
>         return thing + 1
>     else:
>         return -1
> 
> def foo(thing):
> 
>     if thing:
>         return thing + 1
>     return -1
> 
> Obviously both do the same thing.  The first is
> possibly clearer, while the second is more concise.
> 
> Comments?
> 

What about:

def foo(thing):
   if thing:
     result = thing + 1
   else:
     result = -1
   return result

and:

foo = lambda thing: thing and thing + 1 or -1



More information about the Python-list mailing list