reassign to builtin possible !?

Tim Chase python.list at tim.thechases.com
Thu Jan 3 09:05:07 EST 2008


>> But you can't alter the values for True/False globally with this.
> 
> Are you sure ? what about the following example ?
> Is this also shadowing ?
> 
>>>> import __builtin__
>>>> __builtin__.True = False
>>>> __builtin__.True
> False

It doesn't seem to screw things up globally

 >>> import __builtin__
 >>> t = __builtin__.True
 >>> __builtin__.True = False
 >>> __builtin__.False = t
 >>> True
False
 >>> False
True
 >>> 1 == 1
True
 >>> import os
 >>> os.path.isdir('.')
True
 >>> #if they were globally redefined, this would be False
 >>> #you'd have to actually reference __builtin__.True

My thought would be if you do something as daft as 
redefining/shadowing True and False, you get the headaches that 
ensue.  Fortunately, since Python is explicit, you can trace back 
through the code and see where the inanity occurred. 
Additionally, any scoping rules mean that programmer stupidity 
can't leak too badly outside the scope of the block containing 
the stupidity.

It's the old "DIHWIDT! WDDT!" ("Doctor, it hurts when I do 
this!", "well don't do that!") syndrome.

-tkc







More information about the Python-list mailing list