New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
builtins.True exists but can't be accessed #81499
Comments
On Python 3: >>> import builtins
>>> 'True' in dir(builtins)
True
>>> builtins.True
File "<stdin>", line 1
builtins.True
^
SyntaxError: invalid syntax
>>> So "True" is a keyword, I guess this explains the behaviour, but still seems odd to me? Relevant SO question: |
Depending on your mental model of the language, this may not seem odd at all.
>>> # We can do regular string lookups at any time
>>> d['x']
10
>>> d['True']
20
>>> d['for']
30
>>> # globals() isn't special in this regard
>>> globals().update(d)
>>> globals()['x']
10
>>> globals()['True']
20
>>> globals()['for']
30
>>> # Globals is special though in that it provides
>>> # a fast way to do lookups for keys that are
>>> # valid identifiers and are not keywords
>>> x # Fast lookup equivalent to globals['x']
10
>>> True # This is a keyword, so there is no lookup
True
>>> for # This is a keyword, so there is no lookup
SyntaxError: invalid syntax
At any rate, this isn't a bug. It is just the way the language works. Thank you for the report. |
[Raymond]
As far as I can see, the language doesn't need these constants to be in builtins at all - the only reason for keeping them there is backwards compatibility. As an experiment I tried removing It's also fun to note that it's still possible to modify these names: >>> import builtins
>>> builtins.__dict__["True"] = False
>>> builtins.__dict__["True"]
False So I'm not sure what purpose having |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: