Overriding True and False ?

Chris Angelico rosuav at gmail.com
Mon Jan 30 02:57:11 EST 2017


On Mon, Jan 30, 2017 at 6:50 PM, Deborah Swanson
<python at deborahswanson.net> wrote:
> Looks like the moral of the story is that in Python 2.7 you can redefine
> keywords, so long as you don't get any syntax errors after (or during)
> redefinition.

The moral is actually that "True" and "False" aren't keywords in Py2.
They're just built-ins. Compare:

rosuav at sikorsky:~$ python3
Python 3.7.0a0 (default:cebc9c7ad195, Jan 24 2017, 06:55:19)
[GCC 6.2.0 20161027] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1           0 LOAD_CONST               1 (True)
              2 RETURN_VALUE
>>>
rosuav at sikorsky:~$ python
Python 2.7.12+ (default, Sep  1 2016, 20:27:38)
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1           0 LOAD_GLOBAL              0 (True)
              3 RETURN_VALUE
>>>

In Python 2, returning "True" involves a name lookup; in Python 3,
it's simply a constant - a literal.

ChrisA


More information about the Python-list mailing list