Python 字典/映射/哈希的命名约定
虽然其他问题解决了更广泛的类别 序列 和 模块,我问这个非常具体的问题:
“你对字典使用什么命名约定,为什么?”
我一直在考虑的一些命名约定示例:
# '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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
key_to_value
,例如surname_to_salary
当代码中存在密切相关的映射时可能会很有用:a 到 b、b 到 a、c 到 b 等。key_to_value
, for examplesurname_to_salary
may be useful when there are closely interrelated maps in code: a to b, b to a, c to b etc.我似乎从来没有像你提议的那样给它们命名(即保持一种方式)。当我能为哈希找到一个“正确的名称”时,它似乎更加清晰。它可能是“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
, orvalue_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".
values_by_key
value_key_map
那样令人困惑:你不能混淆什么是值名称和键名称
values_by_key
value_key_map
: you can't confuse what isvalue name and what is key name
我认为在字典中的值之后命名字典是有意义的,并删除任何对键的提及。毕竟,您将在像
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 namedkey
well.我将与迄今为止的许多答案(包括所选答案)进行对比,并说:
我避免向变量名称添加诸如
dict
或map
之类的类型引用。对我来说,它感觉太像匈牙利表示法了,感觉非常反Python。要回答我使用什么的问题:
我使用
values
、values_by_key
或values_for_key
形式的名称,以读起来最自然的为准。I will contrast with many answers so far (including the chosen answer) and say:
I avoid adding type references like
dict
ormap
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
orvalues_for_key
, whichever reads most naturally.我通常使用
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.在我们的项目中,我们采用了以下约定:
key_to_value_map
,当它是一个映射aname_dict
时,用于更大更复杂的结构。In our projects, we adopted following convention:
key_to_value_map
when it is a mapaname_dict
for larger more complex structure.