René's URL Explorer Experiment


Title: Replace module level mutable containers with immutable containers · Issue #139003 · python/cpython · GitHub

Open Graph Title: Replace module level mutable containers with immutable containers · Issue #139003 · python/cpython

X Title: Replace module level mutable containers with immutable containers · Issue #139003 · python/cpython

Description: There are many module level containers with constants. By replacing the mutable containers with immutable variants (e.g. replace a set with a frozenset or a list with a tuple) we improve performance (especially in the free-threaded build...

Open Graph Description: There are many module level containers with constants. By replacing the mutable containers with immutable variants (e.g. replace a set with a frozenset or a list with a tuple) we improve performanc...

X Description: There are many module level containers with constants. By replacing the mutable containers with immutable variants (e.g. replace a set with a frozenset or a list with a tuple) we improve performanc...

Opengraph URL: https://github.com/python/cpython/issues/139003

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Replace module level mutable containers with immutable containers","articleBody":"There are many module level containers with constants. By replacing the mutable containers with immutable variants (e.g. replace a set with a frozenset or a list with a tuple) we improve performance (especially in the free-threaded build, see for example https://github.com/python/cpython/pull/138429) and avoid accidental modification of these containers.\n\nThe number of module level lists, dicts and sets on current main is:\n```\nnumber of mutable module level containers by type:\n\u003cclass 'dict'\u003e: 266\n\u003cclass 'list'\u003e: 150\n\u003cclass 'set'\u003e: 63\n\u003cclass 'collections.defaultdict'\u003e: 2\n\u003cclass '_strptime.TimeRE'\u003e: 1\n\u003cclass 'email._encoded_words._QByteMap'\u003e: 1\n```\n\n\u003cdetails\u003e\u003csummary\u003eScript to list all the module level mutable containers\u003c/summary\u003e\n\n```\nimport sys\nimport importlib\nimport pkgutil\nfrom collections import Counter\n\nblacklist = ('idlelib.idle')\nexcluded_submodule_names = ('__main__')\nsearch_submodules = 2\nmutable_containers = (list, dict,  set)\n\ndef list_container_types(module, mcc):\n    print_module = False\n    for a in dir(module):\n        if a in ('__all__', '__path__', '__builtins__', '__annotations__', '__conditional_annotations__'):\n            # why is __all__ a list and not a tuple?\n            continue\n        # if not a.startswith('_'):\n        #    continue\n        attr = getattr(module, a)\n        tp = type(attr)\n        if issubclass(tp, mutable_containers):\n            if not print_module:\n                print(f'{module}:')\n                print_module = True\n            print(f'  {a}: {tp}')\n            mcc.update([tp])\n\n\ndef search_modules(module_names, search_submodules: int, mcc):\n    for name in module_names:\n        if name in blacklist:\n            continue\n        try:\n            module = importlib.import_module(name)\n        except:\n            print(f'{name}: error on import')\n            module = None\n        list_container_types(module, mcc)\n\n        if search_submodules:\n            try:\n                sub_names = list(z.name for z in pkgutil.iter_modules(module.__path__))\n            except Exception as ex:\n                sub_names = []\n            mm = [name + '.' + sub_name for sub_name in sub_names if sub_name not in excluded_submodule_names]\n            search_modules(mm, search_submodules - 1, mcc)\n\nmcc = Counter()\nmodule_names = sorted(list(sys.builtin_module_names)) + sorted(list(sys.stdlib_module_names))\nsearch_modules(module_names, search_submodules=2, mcc = mcc)\n\nprint()\nprint('number of module level containers by type:')\nfor key, value in mcc.items():\n    print(f'{key}: {value}')\n```\n\n\u003c/details\u003e\n\nNot all the mutable containers can be replaced by immutable containers. Some of them need to be mutable (e.g. `copyreg.dispatch_table`). And some of them are part of the public API and we might not want to change the type only for performance reasons.\n\nExample candidates: `_pydatetime._DAYNAMES` (would improve performance of `date.cdate`), `token.EXACT_TOKEN_TYPES`, `xml.etree.ElementTree.HTML_EMPTY`\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-139004\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2025-09-16T19:58:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/139003/cpython/issues/139003"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0f0abc03-f88b-b778-8e1c-09fc78bac8bc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA6AA:258BE8:26DE3D1:3344B28:696B2147
html-safe-nonce76c6a3f45fdd0eaa57bbaf177228dc0485bea34b91baba2bb254d347cd76fadd
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkFBOjI1OEJFODoyNkRFM0QxOjMzNDRCMjg6Njk2QjIxNDciLCJ2aXNpdG9yX2lkIjoiNzYwNTQ5MDgwNDkxNDMzMTk3NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac21fe8d1782e4a7b6bcb1fedcb238dfd244ed06778619f8ab869c6c15876e470d
hovercard-subject-tagissue:3423575403
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/139003/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c3c0635bc3205dc34bed1b4c6fe6a62ef7f82d873e1cbda9b087a3d163ce7e93/python/cpython/issues/139003
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c3c0635bc3205dc34bed1b4c6fe6a62ef7f82d873e1cbda9b087a3d163ce7e93/python/cpython/issues/139003
og:image:altThere are many module level containers with constants. By replacing the mutable containers with immutable variants (e.g. replace a set with a frozenset or a list with a tuple) we improve performanc...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameeendebakpt
hostnamegithub.com
expected-hostnamegithub.com
None5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release82560a55c6b2054555076f46e683151ee28a19bc
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/139003#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F139003
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F139003
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/139003
Reloadhttps://github.com/python/cpython/issues/139003
Reloadhttps://github.com/python/cpython/issues/139003
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/139003
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/139003
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/139003
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/139003
Replace module level mutable containers with immutable containershttps://github.com/python/cpython/issues/139003#top
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
https://github.com/eendebakpt
https://github.com/eendebakpt
eendebakpthttps://github.com/eendebakpt
on Sep 16, 2025https://github.com/python/cpython/issues/139003#issue-3423575403
#138429https://github.com/python/cpython/pull/138429
gh-139003: Use frozenset for module level attributes in _pyrepl.utils #139004https://github.com/python/cpython/pull/139004
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.