[TriZPUG] Two True

Chris Calloway cbc at unc.edu
Fri May 31 02:07:57 CEST 2013


On 5/30/2013 7:01 PM, Philip Semanchuk wrote:
> True in both Python 2 & 3. At least in Python 3 you can't do this nonsense anymore:

>>> True = False
>>> bool(True)
False
>>>

That's what made me think of it. I remembered looking into what made 
that happen in 2 (making a new True identifier in the current namespace 
instead of rebinding the True identifier in the __builtins__ namespace). 
In 3, True, False, and None are keywords and not identifiers, so trying 
to rebind them is a SyntaxError.

Albeit keywords which parse into Python objects. That is, you can still 
do type(False) and dir(True) in 3, whereas type(continue) or dir(while) 
still don't make any sense. :) I don't know if there's any less 
nonsense, though, because in 3, True, False, and None are still shown in 
the __builtins__ namespace if you dir(__builtins__). But you can no 
longer access them as in 2 with __builtins__.True. __builtins__.True is 
also a syntax error in 3. I thought it would have made more sense to 
wall off rebinding the True, False, and None identifiers than walling 
off access to the objects through their namespaces via parsing error, 
which still allows me to do this "nonsense" because no parsing of the 
True keyword take place in this example in 3:

 >>> vars(__builtins__)['True'] = False
 >>> vars(__builtins__)['True']
False
 >>>

Bad implementation of good intentions in 3 if you ask me.

And yet, even more so, after doing the above nonsense in 3:

 >>> True
True
 >>> vars(__builtins__)['True']
False
 >>>

Is vars(__builtins__) being intern'd? Even if vars is returning a copy 
of the __builtins__ namespace, it's consistently returning the *same* 
copy every time. So is the True keyword even fetching the True singleton 
object from the __builtins__ namespace anymore? And if not, why is True 
still in the __builtins__ namespace? Are var and dir even returning str 
names of identifiers of objects in namespaces anymore? Or do they 
sometimes "str names of keywords" depending on what attribute name of 
the namespace is being requested? Inquiring minds want to know if there 
is any internal consistency going on here other than stupid parsing tricks.

Anyway, I remembered back when finding out then that booleans in Python 
are actually number types deep under the covers. And so I wondered if 
they had __add__ methods and what would happen if they were added. Then 
I tested it in 2 & 3 and it was the same. It's kind of like integer 
overflow in 2 where operations on ints resulting in quantities larger 
than sys.maxint get coerced into a long.

So now in 3 we have keywords which *appear* to have methods. Float me. 
Python has a yang. Same day, different nonsense. Meet the new boss. 
Gabba gabba hey.

-- 
Sincerely,

Chris Calloway http://nccoos.org/Members/cbc
office: 3313 Venable Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599


More information about the TriZPUG mailing list