问题描述
虽然其他问题解决了和,我问这个非常具体的问题:
While other questions have tackled the broader category of sequences and modules, I ask this very specific question:
您对字典使用什么命名约定,为什么?
我一直在考虑的一些命名约定示例:
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?
编辑:
选择答案: value_key_map
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.
推荐答案
我从不似乎给他们起了类似您所提议的名称(即保持一种方式)。当我可以找到哈希的专有名称时,似乎更加清楚了。它可能是 person_details, file_sizes或 album_tracks等(尽管后两个似乎具有key_value名称,但第一个要少一些)。在极少数情况下,如果重要的是地图,它将是 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".
这篇关于字典/地图/哈希的Python命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!