Title: Allow `default` keyword argument in dict.get() method · Issue #124675 · python/cpython · GitHub
Open Graph Title: Allow `default` keyword argument in dict.get() method · Issue #124675 · python/cpython
X Title: Allow `default` keyword argument in dict.get() method · Issue #124675 · python/cpython
Description: Feature or enhancement Proposal: Background The dict.get() method is a commonly used feature in Python for retrieving values from dictionaries with a fallback option. Currently, it accepts positional arguments for the key and default val...
Open Graph Description: Feature or enhancement Proposal: Background The dict.get() method is a commonly used feature in Python for retrieving values from dictionaries with a fallback option. Currently, it accepts position...
X Description: Feature or enhancement Proposal: Background The dict.get() method is a commonly used feature in Python for retrieving values from dictionaries with a fallback option. Currently, it accepts position...
Opengraph URL: https://github.com/python/cpython/issues/124675
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow `default` keyword argument in dict.get() method","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n## Background\n\nThe `dict.get()` method is a commonly used feature in Python for retrieving values from dictionaries with a fallback option. Currently, it accepts positional arguments for the key and default value. However, allowing a keyword argument for the default value could enhance code readability and align with Python's philosophy of explicit naming.\n\n## Current Behavior\n\nConsider the following code:\n\n```python\nmy_dict = {\"a\": 1, \"b\": 2, \"c\": 3}\nprint(my_dict.get(\"b\", \"Not found\"))\n```\n\nThis works as expected. However, when attempting to use a keyword argument:\n\n```python\nprint(my_dict.get(\"b\", default=\"Not found\"))\n```\n\nIt raises a `TypeError`:\n\n```\nTraceback (most recent call last):\n File \"/path/to/script.py\", line 3, in \u003cmodule>\n print(my_dict.get(\"b\", default=\"Not found\"))\nTypeError: dict.get() takes no keyword arguments\n```\n\n## Proposed Enhancement\n\nAllow the use of the `default` keyword argument in the `dict.get()` method. This would make the code more explicit and easier to read, especially in complex expressions or when the default value is a long expression.\n\nProposed syntax:\n\n```python\nmy_dict.get(\"key\", default=\"default_value\")\n```\n\n## Rationale\n\n1. **Readability**: This change aligns with PEP 20 (The Zen of Python), specifically the principle \"Explicit is better than implicit.\" Using a keyword argument makes the intention clearer, especially when the default value is complex or when the method call is part of a larger expression.\n\n2. **Consistency**: Many Python built-in functions and methods allow both positional and keyword arguments for optional parameters (e.g., `sorted(iterable, key=None, reverse=False)`). This change would bring `dict.get()` in line with this common pattern.\n\n3. **Backwards Compatibility**: This change would be backwards compatible, as it only adds functionality without altering existing behavior.\n\n4. **Code Style**: It adheres to PEP 8 guidelines for function calls, allowing for more readable code when line length is a concern.\n\n## Implementation Considerations\n\n- The current positional argument for the default value should still be supported for backwards compatibility.\n- The keyword argument should take precedence if both are provided, with a potential deprecation warning for using both simultaneously.\n\n## Conclusion\n\nIntroducing a `default` keyword argument to `dict.get()` would improve code readability and align with Python's design principles. It represents a small but meaningful enhancement to one of Python's most frequently used methods.\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_","author":{"url":"https://github.com/yuriishizawa","@type":"Person","name":"yuriishizawa"},"datePublished":"2024-09-27T14:51:48Z","url":"https://github.com/python/cpython/issues/124675"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:8c15f4c5-b3a3-afd9-6d59-db32bbcf36c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2E4:197995:289E1DF:3742D96:6B004793 |
| html-safe-nonce | 46e5b248a253a1a6057ee6516db9a356260b4b975546cd61e10ae0c852460369 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkU0OjE5Nzk5NToyODlFMURGOjM3NDJEOTY6NkIwMDQ3OTMiLCJ2aXNpdG9yX2lkIjoiNzI5OTExNTU5MjEyMjkxMDYxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5c7182ba8edb1d6a00f2d065aec09fd71ac789107d4ed605b1dd31af74c9d521 |
| hovercard-subject-tag | issue:2553132556 |
| 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/124675/issue_layout |
| twitter:image | https://opengraph.githubassets.com/53088a9365977cb490fb343d6769989bc59f3659aecad22ffa5e5999d8fadf84/python/cpython/issues/124675 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/53088a9365977cb490fb343d6769989bc59f3659aecad22ffa5e5999d8fadf84/python/cpython/issues/124675 |
| og:image:alt | Feature or enhancement Proposal: Background The dict.get() method is a commonly used feature in Python for retrieving values from dictionaries with a fallback option. Currently, it accepts position... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yuriishizawa |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width