(no subject)

Piet van Oostrum piet at cs.uu.nl
Sat Feb 15 11:13:12 EST 2003


>>>>> mis6 at pitt.edu (M) wrote:

M> Subject: adding to __builtins__
M> Consider the following script (I need it under Python 2.2, where True and
M> False are undefined):

M> --- begin truefalse.py --

M> __builtins__.True=1
M> __builtins__.False=0 

M> print True,False

M> --- end truefalse.py --


M> It works:

M> $ python truefalse.py
M> 1 0

M> However, if I import the script I have an AttributeError:

M> python
M> Python 2.2 (#1, Apr 12 2002, 15:29:57)
M> [GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
M> Type "help", "copyright", "credits" or "license" for more information.
>>>> import truefalse
M> Traceback (most recent call last):
M>   File "<stdin>", line 1, in ?
M>   File "truefalse.py", line 3, in ?
M>     __builtins__.True=1
M> AttributeError: 'dict' object has no attribute 'True'

M> What's happening and is there a workaround ?

In the first case __builtins__ is the module __builtin__, in the second
case it is a dictionary. I don't know why, but it probably is documented
somewhere.

This brings the solutions:

import __builtin__

__builtin__.True=1
__builtin__.False=0 

print True,False

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl




More information about the Python-list mailing list