Plain text dictionary keys?

Hi all!

I work with Python and JavaScript pretty often and the thing that trips me up when switching between the two is the use of key-value pairs. JavaScript objects allows plain text keys (which are strings under the hood), and I think that’s a reasonable assumption which saves a lot of key strokes. I believe Python could benefit from something similar:

regular_dict = {
    'name': 'Python',
    'version': 3,
    'icon': '🐍'
}

plain_text_dict = {
    name: 'Python',
    version: 3,
    icon: '🐍'
}

I am aware of this use of dict() that has a similar behavior, but I think it’s still more keystrokes than necessary. Plus, you have to switch from : to =, and from {} to ():

plain_text_dict = dict(
    name='Python',
    version=3,
    icon='🐍'
)

I would like to know what are your thoughts on this?

A non-starter, I’m afraid. Would break existing code that relies on being able to set dictionary keys from variables:

key1 = "name"
mydict = {key1: "bob"}
6 Likes

Given:

{name: value}

how is the interpreter supposed to distinguish between the string "name" and the named variable name? What if there is a conflict between them?

More importantly, how is the human reader supposed to tell which is meant?

Code is written once, but read many times. That 0.1 second that you save by not having to type the quotation marks, will almost certainly cost the reader multiple seconds trying to work out whether name refers to the string or the variable (which may or may not even exist) every time they read it.

2 Likes

A syntax that could work is

regular_dict = {
    'name': 'Python',
    'version': 3,
    'icon': '🐍'
}

plain_text_dict = {
    name='Python',
    version= 3,
    icon='🐍',
}
2 Likes

In addition to the issues people have already mentioned, there’s a broader principle here which is worth mentioning. While Python happily borrows ideas and constructs from other languages, simply copying another language’s syntax for something that Python can already do, is much less likely to happen.

In particular, “it’s easier for people coming from language X” isn’t a very compelling argument. There are a lot of differences between Python and whatever other language you care to consider. Making one specific construct similar doesn’t really help in any significant sense - you still need to learn all of the rest of Python. And often the difference is superficial anyway (Python’s dictionaries are semantically very different from Javascript’s objects, or Lua’s tables, or any other similar data structure).

So I’m afraid I’m -1 on this. Just use dict(x=1, y=2) if you prefer the keyword approach.

11 Likes

That makes sense. I forgot that Python allows defined variables to be used as keys. I haven’t encountered it as much, so I didn’t consider it when I wrote this post.

I was thinking that since it was inside {}, the interpreter would interpret the plain text within those brackets as strings:

x = 5

some_dict = {
    x: 'Hello'   # 'x': 'Hello'
}

print(x) # 5
print(some_dict.x) # Hello

But, as it was pointed out in another comment: pre-defined variables can be used as keys, so my initial thought wouldn’t work.

I think this is a good middle ground!

As someone whose recently had to learn groovy for Jenkins pipelines, I can say with absolute certainty that this feature, while it may seem appealing at first, is so darn confusing and I freaking hate it.

Im about 4 days into this but trying to make sense of that syntax is non intuitive

1 Like

It is already possible to achieve this behaviour in Python without having to change the syntax of the language.

Is it? How so?

dict(a=1, b=2)

The “I am aware” part of the first post already covered and dismissed that, so that can’t be it.

Thanks, we did discuss that method of defining key-value pairs in the initial post.
I was hoping the previous post was referring to another way to create dictionaries that hasn’t
been mentioned yet.