True

Martin v. Löwis martin at v.loewis.de
Thu Aug 7 19:03:21 CEST 2003


Peter Hansen <peter at engcorp.com> writes:

> As Skip said... but it should have had a comment anyway, since it
> wasn't clear.  You could also consider using "not not boolVal" if
> you want to make the code _slightly_ (IMHO) more readable, and slightly
> slower, but I think one still needs a comment explaining it. :-(

I find

def boolean2str(val):
     if val:
          return '1'
     else:
          return '0'

both more readable, and twice as fast. It doesn't need to create a
dictionary each time, and it does not to perform a dictionary lookup.
If you prefer compactness, use

def boolean2str(val, results=('1', '0')):
     return results[not val]

Regards,
Martin




More information about the Python-list mailing list