(no subject)

Alex Martelli aleax at aleax.it
Fri Jan 31 17:16:37 EST 2003


Skip Montanaro wrote:

> How about
> 
>     import __builtin__
>     try:
>         __builtin__.True
>     except AttributeError:
>         __builtin__.True = 1
>         __builtin__.False = 0
> 
> ?  (The builtin module is not plural.)

Much as I like the "better to ask permission than forgiveness" idiom in 
general, it doesn't smell right here.  What about:


import __builtin__
if not __builtin__.hasattr('True'):
    __builtin__.True = 1
if not __builtin__.hasattr('False'):
    __builtin__.False = 0


You can omit the second if you prefer, to get exactly the same
logic as in your snippet -- that way it's even more concise
(but, I dunno, I like the symmetry here).

The suggestion of using e.g 1==1 instead of 1 as the value
for True, which I've seen elsewhere in the thread, seems
a bit, well, "over-cute" to me -- in Python 2.3, where 1==1
is not exactly the same thing as 1, True IS built-in anyway...


Alex





More information about the Python-list mailing list