correct way to check for True in __builtins__?

Chris Liechti cliechti at gmx.net
Fri May 10 21:11:36 EDT 2002


"Mark McEahern" <mark at mceahern.com> wrote in 
news:mailman.1021073299.24385.python-list at python.org:

> I want to check whether True is builtin and I thought I'd be clever by
> saying:
> 
>      if True not in __builtins__:
>           True = bool(1)
>           False = bool(0)
> 
> of course, __builtins__ doesn't seem to be built to be accessed that way.

well its a module. try this:

if 'True' in __builtins__.__dict__:
   ...

(2.2 and up, add a ".keys()" for backwars compatibility)

[...]
> Or:
>      if True in dir(__builtins__):
>           ...

which is basicaly the same as looking at ".__dict__.keys()"
(True should be a string 'True', otherwise you get a NameError if it isn't 
defined and always a false result otherwise)

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list