Mas Bagol
Mas Bagol

Reputation: 4627

What's the difference between True and False in python grammar?

I'm reading the python language specification and found there's a None, True, and False token. I can understand the difference between None and False since None is not a boolan. But, about True and False, why not just BOOLEAN there? Is there any case where True and False behave differently? Or is there any semantical difference?

Note that I'm asking about the difference in grammar not the boolean value which is obviously different.

Upvotes: 1

Views: 147

Answers (3)

holdenweb
holdenweb

Reputation: 37163

Presumably from the reference to which you linked you are referring to the grammatical production

atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [testlist_comp] ']' |
       '{' [dictorsetmaker] '}' |
       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')

This simply gives the strings None, True, and False the same status as certain other language elements. While it would have been possible to create a definition of a Boolean atom which could either be True or False and use that in the grammar, what purpose would that have served?

In point of fact, even in 2.7 you can try to delete the True and False definitions from the __builtins__ namespace:

>>> del __builtins__.True
>>> del __builtins__.False

Traceback (most recent call last):
  File "/Users/sholden/.virtualenvs/jlab/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
NameError: global name 'True' is not defined

Interestingly, if you delete False first there is no complaint about the deletion of True :-)

However, this makes even quite standard Python make no sense at all:

>>> 1 == 1

Traceback (most recent call last):
  File "/Users/sholden/.virtualenvs/jlab/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
NameError: global name 'True' is not defined

This underlines that Python was produced as "a programming language for consenting adults." Ultimately it gives you enough rope to hang yourself if you are determined to do that.

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 362107

atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [testlist_comp] ']' |
       '{' [dictorsetmaker] '}' |
       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')

NAME, NUMBER, and STRING are tokens representing three classes of tokens. Each of them represents an unlimited set of possible tokens. There are many number literals that can be classified as NUMBER, many string literals that can be STRINGs, etc.

There are only two boolean literals, True and False. The tokenizer could have been written to classify them both as BOOLEAN. Could have, but wasn't. They're only referred to once in the entire grammar, so writing 'True' | 'False' is no big deal.

Upvotes: 3

wim
wim

Reputation: 363476

This is a formalization of the fact that True and False are special names in python3: you can't assign to them.

The reason that they aren't BOOLEAN is simply that boolean isn't a valid token for the parser.

Note: You will find this detail missing in the python2 grammar, where you can actually assign to the names True and False (...if you want to watch the world burn).

Upvotes: 3

Related Questions