To add a little more to your own answer (there should be a comment, but formatting is required for a long time):
python2.7 ... >>> import __builtin__ >>> id(True) 7744528 >>> id(__builtin__.True) 7744528 >>> True = 'abc' >>> id(True) 34386540544
The value from id is the (essentially) internal identifier or "true name" of the object in Python, if you will. (This literally turns the C pointer into an integer.) The is test compares the identifier of an object.
>>> 1==1 True >>> id(1==1) 7744528
This shows that the logical result of the comparison is the “old” True , which is still available as __builtin__.True .
You have re-linked the name __main__.True (your current module at the prompt of the interpreter >>> __main__ ):
>>> True 'abc' >>> __builtin__.True True
and
>>> import __main__ >>> id(__main__.True) 34386540544 >>> __main__.True 'abc' >>>
The same thing happens in Python beginner programs when they write functions such as:
def foo(list): ...
list is a built-in function, but inside the foo function, the name is bound with an argument. Then somewhere in the piece ... they get a surprise:
x = list(y)
They expect this to call __builtin__.list , but instead try to call their local variable as a function.
(It is possible, but not generally a good style, to import __builtin__ and instead name things through these names. It is also possible to strip the names __builtin__ , but this is even worse. :-))
torek
source share