Skip to content
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

Closed
chris-rands mannequin opened this issue Jun 17, 2019 · 3 comments
Closed

builtins.True exists but can't be accessed #81499

chris-rands mannequin opened this issue Jun 17, 2019 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@chris-rands
Copy link
Mannequin

chris-rands mannequin commented Jun 17, 2019

BPO 37318
Nosy @rhettinger, @mdickinson, @chris-rands

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:

assignee = None
closed_at = <Date 2019-06-17.22:07:56.918>
created_at = <Date 2019-06-17.15:17:02.923>
labels = ['type-bug', 'invalid']
title = "builtins.True exists but can't be accessed"
updated_at = <Date 2019-06-18.15:59:27.078>
user = 'https://github.com/chris-rands'

bugs.python.org fields:

activity = <Date 2019-06-18.15:59:27.078>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2019-06-17.22:07:56.918>
closer = 'rhettinger'
components = []
creation = <Date 2019-06-17.15:17:02.923>
creator = 'ChrisRands'
dependencies = []
files = []
hgrepos = []
issue_num = 37318
keywords = []
message_count = 3.0
messages = ['345863', '345927', '345993']
nosy_count = 3.0
nosy_names = ['rhettinger', 'mark.dickinson', 'ChrisRands']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue37318'
versions = []

@chris-rands
Copy link
Mannequin Author

chris-rands mannequin commented Jun 17, 2019

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:
https://stackoverflow.com/questions/56633402/why-are-true-and-false-being-set-in-globals-by-this-code

@chris-rands chris-rands mannequin added the type-bug An unexpected behavior, bug, or error label Jun 17, 2019
@rhettinger
Copy link
Contributor

Depending on your mental model of the language, this may not seem odd at all.

>> # We can set any key/value pair in any dictionary
>> d = {'x': 10, 'True': 20, 'for': 30}

>>> # 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.

@mdickinson
Copy link
Member

[Raymond]

It is just the way the language works.

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 True, False and None from bltinmodule.c, and recompiling, and every single test in the test suite passed just fine. So apparently we're not even testing for the existence of these constants.

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 True, False and None in builtins serves.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants