Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Syntax for aliases to keys of python dictionaries #91440

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

Closed
tomlin7 opened this issue Apr 11, 2022 · 3 comments
Closed

Syntax for aliases to keys of python dictionaries #91440

tomlin7 opened this issue Apr 11, 2022 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@tomlin7
Copy link

tomlin7 commented Apr 11, 2022

Description

There are cases where keys of python dictionaries may need aliases. When two keys point to the same value, duplication can also be avoided with this feature.

How it is done currently:

foo = {
    key1: "value",
    key2: "value",
    ...
}

Here "value" is repeatedly used as value of the keys. When there are more keys with the same value, the more duplication occurs.
See the following syntax:

Conceptual syntax

foo = {
    key1, key2, ...: "value"
}

Here, the duplication is less and the code looks cleaner. Do note that the punctuator , is just as per my concept. If it may hold conflicts with the item separators of dictionaries (which is also ,), another punctuator can be used instead. E.g: |, ;, +

Use cases

  • This feature can be extremely useful for libraries which tends to be user friendly.
  • In big projects, when there is a confusion of key names ("was that show or display?"), this feature helps to avoid scrolling all the way up.
  • When key names are long, this feature helps to also accept acronyms or short names.

Examples

>>> foo = {
...     "person1", "p1": "yes",
...     "person2", "p2": "no",
... }
>>>
>>> a["person1"]
'yes'
>>> a["p1"]
'yes'
@AlexWaygood
Copy link
Member

Hi, thanks for the feature request! I'm going to close this issue for now, as a change in syntax of this magnitude should first be discussed in the Python-ideas mailing list or at https://discuss.python.org/c/ideas/6

@AlexWaygood AlexWaygood added the type-feature A feature request or enhancement label Apr 11, 2022
@tomlin7
Copy link
Author

tomlin7 commented Apr 11, 2022

I have opened just opened a topic at discuss.python.org, thank you for responding! I will make sure to follow the instructions you mentioned for future issues 🀝

@AlexWaygood
Copy link
Member

No worries 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants