reassign to builtin possible !?

Diez B. Roggisch deets at nospam.web.de
Thu Jan 3 14:50:27 CET 2008


Bernhard Merkle wrote:

> On Jan 3, 2:07 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> This hal always been possible. But it's not reassigning, it's shadowing -
>> which is a totally different beast. Shadowing builtins is bad style, but
>> lokal to your context. Which can get nasty of course, if you do the above
>> on e.g. module level.
>>
>> 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 ?
> 
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import __builtin__
>>>> __builtin__.True = False
>>>> __builtin__.True
> False
>>>> True
> False

I'm not entirely sure what happens there, but that seems to only work in the
interactive prompt.

--------- test.py ------------


print True

if __builtins__.True == 10:
    print "I'm reassigned globally"
--------- test.py -------------

Then, in the interpreter do:

droggisch at ganesha:/tmp$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times
>>> __builtins__.True = 10
>>> import test
10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 5, in <module>
    if __builtins__.True == 10:
AttributeError: 'dict' object has no attribute 'True'
>>> 


Diez



More information about the Python-list mailing list