None
is a keyword. As such, it can not be used with the dot notation, much like how __builtins__.True
, __builtins__.class
and __builtins__.def
are also a syntax error.
This can be bypassed by using getattr
:
print(getattr(__builtins__, 'None') is None)
# True
print(getattr(__builtins__, 'False') is False)
# True
print(getattr(__builtins__, 'True') is True)
# True
Unlike abs
, len
, zip
etc (which are top-level functions), None
, True
and False
are keywords in Python 3 (see https://docs.python.org/3/reference/lexical_analysis.html#keywords).
In Python 2 True
and False
were not keywords (just a built-in name/constant) (See https://docs.python.org/2/reference/lexical_analysis.html#keywords) so you could reassign to them. As mentioned above this is impossible in Python 3 since they became keywords.
Also see this question: True=False assignment in Python 2.x
With that said, one can still mess around with __builtins__.True
in Python 3, but it will not affect the actual True
as it used to be in Python 2:
print(getattr(__builtins__, 'True'))
# True
setattr(__builtins__, 'True', False)
print(getattr(__builtins__, 'True'))
# False
print(True)
# True