Reputation: 569
The following are true:
'None' in keyword.kwlist
'None' in builtins.__dict__ # import builtins
My understanding:
x
by getting object builtins.__dict__[x]
x
in a special way that depends on what x
isThis implies that Python evals keyword None
to the value of type NoneType
(which is interned) without using builtins.__dict__
. So why does builtins.__dict__
contain 'None'
?
(the same question applies to True
and False
)
Upvotes: 7
Views: 92
Reputation: 532093
Keywords, in general, aren't evaluated at all. The keyword
module just provides, for informational purposes, which words are recognized as special by the Python grammar. In particular, None
, True
, and False
are recognized as (hard) keywords so that attempts to redefine them are syntax errors, rather than runtime errors. Also, the list of keywords is not involved in name lookups at all.
Otherwise, None
is just like any other name that is bound to some value (in this case, the lone value of type types.NoneType
). In particular, the definition is in the built-in scope, so that it is available from any module without needing to import anything first. builtins
just reflects those names that are defined in the built-in scope.
Upvotes: 0