字典/地图/哈希的 Python 命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2405958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Naming Conventions for Dictionaries/Maps/Hashes
提问by pokstad
While other questions have tackled the broader category of sequencesand 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
选择的答案: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.
选择答案的原因:允许代码者快速轻松地找出地图的键和值,以及它是一张地图而无需查看其他任何地方的事实。
采纳答案by viraptor
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.
我似乎从来没有像你提议的那样命名它们(即保持一种方式)。当我能为散列找到一个“正确的名称”时,它似乎更加清晰。它可能是“person_details”或“file_sizes”或“album_tracks”等(尽管最后两个似乎有键值名称,第一个少一点)。在极少数情况下,它会是key_value_map
,或者value_key_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".
我永远不会为此假设任何命名方案。有时,价值就是你所追求的,有时是关键。我的偏好是“一个自然的名字”。
回答by DSblizzard
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.
key_to_value
,例如,surname_to_salary
当代码中有密切相关的映射时可能很有用:a 到 b、b 到 a、c 到 b 等。
回答by unutbu
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.
我认为在 dict 中的值之后命名 dict 并删除对键的任何提及是有意义的。毕竟values[key]
,假设您命名key
得当,您将在这样的情况下使用 dict,这使得键是什么非常清楚。
回答by Ignacio Vazquez-Abrams
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.
我通常使用 <something>map
因为它通常是一个映射,例如字符串到函数,或数字到类,等等。未命名的 dicts 通常以更大的结构结束,所以我不担心它们。
回答by Sklavit
values_by_key
values_by_key
- it is not so confusing as
value_key_map
: you can't confuse what is value name and what is key name - it doesn't name the type directly -- python style naming
- 它并不像
value_key_map
:您不能混淆什么是值名称和什么是键名称 - 它不直接命名类型——python 样式命名
回答by Terrabits
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.
我避免向变量名称添加类似dict
或map
这样的类型引用。这对我来说太像匈牙利符号了,感觉非常反 Python。
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.
我使用values
, values_by_key
or形式的名称values_for_key
,以最自然的方式读取。
回答by L. G.
In our projects, we adopted following convention:
在我们的项目中,我们采用了以下约定:
key_to_value_map
when it is a mapaname_dict
for larger more complex structure.
key_to_value_map
当它是一张地图aname_dict
用于更大更复杂的结构。