Python 字典/映射/哈希的命名约定

发布于 2024-08-24 13:16:02 字数 765 浏览 7 评论 0原文

虽然其他问题解决了更广泛的类别 序列模块,我问这个非常具体的问题:

“你对字典使用什么命名约定,为什么?”

我一直在考虑的一些命名约定示例:

# 'value' is the data type stored in the map, while 'key' is the type of key
value_for_key={key1:value1, key2,value2}
value_key={key1:value1, key2,value2}
v_value_k_key={key1:value1, key2,value2}

不要用“因为我的工作告诉我”来回答“为什么”,这不是很有帮助。推动选择的原因更为重要。除了可读性之外,字典命名约定还有其他好的考虑因素吗?

编辑:

选择的答案: value_key_map

选择答案的原因: 允许代码审阅者快速轻松地找出映射的键和值,事实上,它只是一张地图,无需看其他任何地方。

While other questions have tackled the broader category of sequences and modules, I ask this very specific question:

"What naming convention do you use for dictionaries and why?"

Some naming convention samples I have been considering:

# 'value' is the data type stored in the map, while 'key' is the type of key
value_for_key={key1:value1, key2,value2}
value_key={key1:value1, key2,value2}
v_value_k_key={key1:value1, key2,value2}

Don't bother answering the 'why' with "because my work tells me to", not very helpful. The reason driving the choice is more important. Are there any other good considerations for a dictionary naming convention aside from readability?

EDIT:

Chosen answer: value_key_map

Reason for chosen answer: Allows a code reviewer to quickly and easily figure out the key and value for a map, and the fact that it is a map without looking anywhere else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

街角卖回忆 2024-08-31 13:16:02

key_to_value,例如 surname_to_salary 当代码中存在密切相关的映射时可能会很有用:a 到 b、b 到 a、c 到 b 等。

key_to_value, for example surname_to_salary may be useful when there are closely interrelated maps in code: a to b, b to a, c to b etc.

无人问我粥可暖 2024-08-31 13:16:02

我似乎从来没有像你提议的那样给它们命名(即保持一种方式)。当我能为哈希找到一个“正确的名称”时,它似乎更加清晰。它可能是“person_details”或“file_sizes”或“album_tracks”等(尽管最后两个似乎有键值名称,第一个有点少)。在极少数情况下,它会是 key_value_map,或者 value_key_map(如果它是地图很重要的话)。

我永远不会为此假设任何命名方案。有时,价值观就是您所追求的,有时则是钥匙。我的偏好是“自然的名字”。

I never seem to name them anything like what you proposed (i.e. keeping one way). It just seems to be much more clear when I can find a "proper name" for the hash. It might be "person_details" or "file_sizes" or "album_tracks" etc. (although the last 2 seem to have key_value names, first one a bit less). In rare cases, it will be key_value_map, or value_key_map if it's important that it's a map.

I would never assume any naming scheme for that. Sometimes the values are what you're after, sometimes the keys. My preference is "a natural name".

夜清冷一曲。 2024-08-31 13:16:02

values_by_key

  1. 它并不像 value_key_map 那样令人困惑:你不能混淆什么是
    值名称和键名称
  2. 它不直接命名类型 - python 风格命名

values_by_key

  1. it is not so confusing as value_key_map: you can't confuse what is
    value name and what is key name
  2. it doesn't name the type directly -- python style naming
一个人练习一个人 2024-08-31 13:16:02

我认为在字典中的值之后命名字典是有意义的,并删除任何对键的提及。毕竟,您将在像 values[key] 这样的情况下使用字典,这使得键是什么非常清楚,假设您命名了 key

I think it makes sense to name the dict after the values in the dict, and drop any mention of the key. After all, you are going to be using the dict in situations like values[key] which makes it perfectly clear what the keys are, assuming you named key well.

Bonjour°[大白 2024-08-31 13:16:02

我将与迄今为止的许多答案(包括所选答案)进行对比,并说:

我避免向变量名称添加诸如 dictmap 之类的类型引用。对我来说,它感觉太像匈牙利表示法了,感觉非常反Python。

要回答我使用什么的问题:

我使用 valuesvalues_by_keyvalues_for_key 形式的名称,以读起来最自然的为准。

I will contrast with many answers so far (including the chosen answer) and say:

I avoid adding type references like dict or map to the variable name. It feels too much like Hungarian notation to me, which feels very anti-pythonic.

To answer the question of what I DO use:

I use names of the form values, values_by_key or values_for_key, whichever reads most naturally.

红尘作伴 2024-08-31 13:16:02

我通常使用map 因为它通常是一个映射,例如字符串到函数,或数字到类,等等。未命名的字典通常最终会出现在更大的结构中,所以我不担心它们。

I usually use <something>map since it's usually a map such as strings to functions, or numbers to classes, or whatnot. Unnamed dicts usually end up in a larger structure, so I don't worry about them.

浅唱々樱花落 2024-08-31 13:16:02

在我们的项目中,我们采用了以下约定:

  • key_to_value_map,当它是一个映射
  • aname_dict时,用于更大更复杂的结构。

In our projects, we adopted following convention:

  • key_to_value_map when it is a map
  • aname_dict for larger more complex structure.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文