-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Allow default
keyword argument in dict.get() method
#124675
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
Comments
Yes but there is also "Although practicality beats purity." and "There should be one-- and preferably only one --obvious way to do it.". So, I wouldn't take the Zen as the holy scriptures in this case.
Changing
It would not change the behaviour but it could affect performances. Adding
Using both simulteneously should be rejected. There shouldn't be any warning IMO.
This actually increases the line length if you use the keyword argument. Personally, I'm -1 on this one. While it is great to improve readability, knowing that the second argument of
I don't feel it is a minor feature engouh and I feel we should ask a wider community on Discourse: https://discuss.python.org/c/ideas/6. I will leave the issue open for a few days but I think this one should be closed until a consensus has been reached both among core devs and the community (before posting on Discourse, please check if there aren't existing topics on this matter). |
I agree with @picnixz viewpoint. Personally, I believe that methods of built-in types should not rely too heavily on keyword arguments; they should be as simple and intuitive as possible, rather than pursuing readability at the expense of practicality (frankly, I don’t want to pass a bunch of parameters when using methods of built-in types, it would make me feel tired). |
Regarding the complex expressions you mentioned, I have written a relatively complex one that uses a lot of f-strings (personally, I think it might not get much more complex than this) dict.get(
f"{ip.a}.{ip.b}.{ip.c}.{ip.d}",
f"{ip.a}.{ip.b}.{ip.c}.{ip.d} not found"
) It seems that this doesn’t make the code difficult to read and also keeps it neat. |
Hi @yuriishizawa! Please don't use ChatGPT or other LLMs to generate feature proposals -- they generally form proposals that are hard to follow and make irrelevant points. With that being said, I've needed this exact feature before, but we don't need a new keyword argument; If you really do feel that this is something that's needed, bring it up on DPO. Thanks! |
I'd like to throw in another argument for implementing this feature: The current implementation is contrary to the Python documentation which says:
This syntax and wording implies that both key and default can be passed as keyword arguments. On the other hand, the CPython built-in help correctly includes a slash at the end, implying that no keyword arguments are accepted:
It might make sense to update the Python documentation to match the actual behavior, especially if there is consensus among major Python implementations like CPython, PyPy, IronPython, etc. to not allow default as a keyword argument. |
I don't think so. There's nothing there that mentions anything about whether it can be passed as a kwarg or not. Again, there's enough contention here that this won't fly on GitHub--propose it on DPO, and see if it gets any additional community support, then we can reconsider this. |
The lack of a slash in the function signature implies that all parameters can be passed as arguments or keyword arguments. Hence, the way the Python language is specified in its documentation,
What's DPO? This Github repo is the official issue tracker for CPython as per the docs. |
DPO refers to discuss.python.org. Should have clarified, sorry! All feature discussion is supposed to happen elsewhere, the issue tracker is used for tracking bugs and features that have already been accepted. |
This is basically a matter of fully reflecting PEP 457 and PEP 570 in the documentation. I bet the same issue affects many more functions and methods of the standard library. I do think that ambiguities like this one are in fact serious, as they inhibit the portability of Python Code between CPython, PyPy, IronPython and other implementations. |
If there's a docs issue, then that's fixable. |
Even changing the docs requires a discussion for two reasons:
|
I have created a PR for the docs difference at #128207. Mentioned in that PR is the fact that not accepting keyword arguments means that cpython/Lib/_collections_abc.py Line 808 in 6f3c2c8
The documentation is already inconsistent with regards to this - that is, some function definitions have
Are you suggesting that maybe the docs.python.org documentation should mention the differences between CPython and PyPy here?
I think that the documentation should be updated and then if the implementation is changed, the documentation should be updated again to reflect reality. |
I have since changed my mind, after reading this from the PR (gh-128207):
A dictionary is the de-facto |
I think the issue is more about the fact that
I don't know whether it will be faster or slower, but I'd advise checking performances first (even if there is a (small but not insurmountable) inconsistency). |
I'd support allowing |
Yes. This is either CPython implementation detail or a longstanding bug in CPython, so it should be documented as such. I view docs.python.org as the definition of an abstract language called Python. I don't see a reason why Python (this ideal abstract language) should suddenly stop accepting keyword arguments in
Doing one function at a time risks adding even more inconsistencies, as it is not obvious what the proper course of action should be. |
Agree. But what about |
Uh oh!
There was an error while loading. Please reload this page.
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 value. However, allowing a keyword argument for the default value could enhance code readability and align with Python's philosophy of explicit naming.Current Behavior
Consider the following code:
This works as expected. However, when attempting to use a keyword argument:
It raises a
TypeError
:Proposed Enhancement
Allow the use of the
default
keyword argument in thedict.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.Proposed syntax:
Rationale
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.
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 bringdict.get()
in line with this common pattern.Backwards Compatibility: This change would be backwards compatible, as it only adds functionality without altering existing behavior.
Code Style: It adheres to PEP 8 guidelines for function calls, allowing for more readable code when line length is a concern.
Implementation Considerations
Conclusion
Introducing a
default
keyword argument todict.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.Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: