user615536
user615536

Reputation: 569

Why is 'None' in builtins.__dict__

The following are true:

My understanding:

This 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

Answers (1)

chepner
chepner

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

Related Questions