# Plain text dictionary keys?

**URL:** <https://discuss.python.org/t/plain-text-dictionary-keys/20925>\
**Category:** Ideas\
**Created:** [November 10, 2022, 8:40am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925 "2022-11-10T08:40:40Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 10, 2022, 8:40am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/1 "2022-11-10T08:40:40Z")

</div>

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:

```auto
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 **()**:

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

```

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

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [November 10, 2022, 9:09am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/2 "2022-11-10T09:09:38Z")

</div>

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

```python
key1 = "name"
mydict = {key1: "bob"}

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [November 9, 2022, 12:07pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/3 "2022-11-09T12:07:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![\_builtin.self](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/_builtin.self/32/31874_2.png) [@\_builtin.self](https://discuss.python.org/u/_builtin.self)\
**Post date:** [November 10, 2022, 10:25am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/4 "2022-11-10T10:25:00Z")

</div>

A syntax that could work is

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

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

```

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [November 10, 2022, 12:03pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/5 "2022-11-10T12:03:52Z")

</div>

> [@jerryq27](#):
>
> I would like to know what are your thoughts on this?

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.

---

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 10, 2022, 4:38pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/6 "2022-11-10T16:38:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 10, 2022, 4:43pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/7 "2022-11-10T16:43:56Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 10, 2022, 4:44pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/8 "2022-11-10T16:44:40Z")

</div>

I think this is a good middle ground!

---

<div class="post-metadata">

**Author:** ![Melendowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/melendowski/32/2960_2.png) [@Melendowski](https://discuss.python.org/u/Melendowski)\
**Post date:** [November 11, 2022, 2:55am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/9 "2022-11-11T02:55:17Z")

</div>

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.

> **[Maps in Groovy | Baeldung](https://www.baeldung.com/groovy-maps)**
>
> Explore the core operations of Maps in Groovy

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

---

<div class="post-metadata">

**Author:** ![Resuscitating](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/resuscitating/32/8782_2.png) [@Resuscitating](https://discuss.python.org/u/Resuscitating)\
**Post date:** [November 17, 2022, 3:52am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/10 "2022-11-17T03:52:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 19, 2022, 8:23am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/11 "2022-11-19T08:23:58Z")

</div>

Is it? How so?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [November 19, 2022, 10:02am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/12 "2022-11-19T10:02:53Z")

</div>

`dict(a=1, b=2)`

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [November 19, 2022, 11:44am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/13 "2022-11-19T11:44:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jerryq27](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jerryq27/32/9720_2.png) [@jerryq27](https://discuss.python.org/u/jerryq27)\
**Post date:** [November 21, 2022, 2:52am UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/14 "2022-11-21T02:52:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Resuscitating](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/resuscitating/32/8782_2.png) [@Resuscitating](https://discuss.python.org/u/Resuscitating)\
**Post date:** [July 18, 2023, 5:03pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/15 "2023-07-18T17:03:02Z")

</div>

# Brief introduction/apology

Hello there! I’m sorry for the very late response, I don’t check this website/account very often. I hope you’re not too upset about the wait.

# Note the following caveats

– Any vagaries of your encoding/locale may change the results (because the raw string in the text will be interpreted differently).

– The following conditions must be true for this to work:

- The keystring is not the name of a defined variable that is reachable from that scope (otherwise it’ll just use the value of that variable instead of the literal string that you used).
- The keystring is a valid variable name (no invalid syntax here, please).
- You must be using cPython, or some 100% cPython compatible implementation.

# Now as promised, here you go!

```python
##
# Imports
##

# Used for checking if Py_TRACE_REFS is on. If Python was configured with
# `./configure --with-trace-refs' (the proper way to enable Py_TRACE_REFS) then
# `sys.getobjects' will be defined.
# <https://docs.python.org/3/using/configure.html#cmdoption-with-trace-refs>
import sys

# Used for shenanigans :-)
from ctypes import (
    py_object as PyObject_p,
    c_ssize_t as Py_ssize_t,
    c_uint32 as Py_uint32_t,
    sizeof as sizeof,
    Union as union,
)

##
# Hook definition.
##

class __globals__ (dict):
    # New definition for `globals(). __getitem__'
    def __getitem__ (self, item):
        if item in self:
            return {}. __class__. __getitem__ (self, item)
        _b = self[" __builtins__"]
        if _b. __class__ is not {}. __class__ :
            _b = _b. __dict__
        if item in _b:
            return _b[item]
        return item

## 
# Find the offset to the `ob_type' member of the `PyObject' struct.
# <https://github.com/python/cpython/blob/main/Include/object.h#L166-L175>
##

# `Py_TRACE_REFS' stuff.
_ob_next = PyObject_p * hasattr(sys, "getobjects")
_ob_prev = PyObject_p * hasattr(sys, "getobjects")
# `ob_refcnt' union stuff.
class ob_refcnt_union(union):
    _fields_ = (
        ("ob_refcnt", Py_ssize_t),
        ("ob_refcnt_split", Py_uint32_t * 2),
    )
# `ob_type' is right after the refcounting stuff.
ob_type_offset = sizeof(_ob_next) + sizeof(_ob_prev) + sizeof(ob_refcnt_union)

##
# Hook the object type.
##
PyObject_p.from_address(
    id(globals()) + ob_type_offset
).value = __globals__

##
# Now it's hooked :-)
##
print(hello, world)
print({
    this_is: a_dictionary,
})

```

# Bye

Hope you enjoy playing around with this, and have a nice day!

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [July 18, 2023, 10:19pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/16 "2023-07-18T22:19:12Z")

</div>

That is very cool and very horrifying! Thanks for sharing

---

<div class="post-metadata">

**Author:** ![Melendowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/melendowski/32/2960_2.png) [@Melendowski](https://discuss.python.org/u/Melendowski)\
**Post date:** [July 18, 2023, 10:27pm UTC](https://discuss.python.org/t/plain-text-dictionary-keys/20925/17 "2023-07-18T22:27:03Z")

</div>

This has been discussed before, I forget where, I commented then about this being a feature in Groovy (some java bastardization for scripting Jenkins software). This is a horrible feature and will do nothing but add a bunch of overhead and slowness to dicts.

Edit: ha, didn’t realize this was the same thread, clicking in on mobile made this seem like I was replying to OP
