Title: List comprehensions now have access to the enclosing class scope · Issue #104374 · python/cpython · GitHub
Open Graph Title: List comprehensions now have access to the enclosing class scope · Issue #104374 · python/cpython
X Title: List comprehensions now have access to the enclosing class scope · Issue #104374 · python/cpython
Description: Code: class X: y = 1 print([x + y for x in range(3)]) On current main: Python 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for mo...
Open Graph Description: Code: class X: y = 1 print([x + y for x in range(3)]) On current main: Python 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "c...
X Description: Code: class X: y = 1 print([x + y for x in range(3)]) On current main: Python 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help...
Opengraph URL: https://github.com/python/cpython/issues/104374
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"List comprehensions now have access to the enclosing class scope","articleBody":"Code:\r\n\r\n```\r\nclass X:\r\n y = 1\r\n print([x + y for x in range(3)])\r\n```\r\n\r\nOn current main:\r\n\r\n```\r\nPython 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n\u003e\u003e\u003e class X:\r\n... y = 1\r\n... print([x + y for x in range(3)])\r\n... \r\n[1, 2, 3]\r\n```\r\n\r\nBut on 3.11:\r\n\r\n```\r\n\u003e\u003e\u003e class X:\r\n... y = 1\r\n... print([x + y for x in range(3)])\r\n... \r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"\u003cstdin\u003e\", line 3, in X\r\n File \"\u003cstdin\u003e\", line 3, in \u003clistcomp\u003e\r\nNameError: name 'y' is not defined\r\n```\r\n\r\nI actually like the new behavior, and I discovered this bug as I was preparing a post proposing that we make things work this way in 3.13. But it's a behavior change, and we should only make this change on purpose and not by accident.\r\n\r\nThis could plausibly break real code if the name is defined in both the module and class scope, and current code relies on getting the name from the module scope.\r\n\r\ncc @carljm \n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104519\n* gh-104528\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2023-05-11T00:30:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/104374/cpython/issues/104374"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:afd1f98f-2ceb-3ac5-b78b-6d6364ee0f33 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 966E:31315B:86351D:B430B6:6969BE8F |
| html-safe-nonce | 2b18cba812d9557c7a31164542b4d31b213caa24b1e0575b4bda238284b2f178 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjZFOjMxMzE1Qjo4NjM1MUQ6QjQzMEI2OjY5NjlCRThGIiwidmlzaXRvcl9pZCI6IjYyMTU5MzAzMDc1Mzg0MzU3MjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 60fd1f1065076295b159d94fe0b85dd4a281aae7bea8978e9cd620df386fc686 |
| hovercard-subject-tag | issue:1704785645 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/104374/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1cde81877d2afcedb0eb753547c67509a95ad10081aa8bae18c92c7346117680/python/cpython/issues/104374 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1cde81877d2afcedb0eb753547c67509a95ad10081aa8bae18c92c7346117680/python/cpython/issues/104374 |
| og:image:alt | Code: class X: y = 1 print([x + y for x in range(3)]) On current main: Python 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width