字典的单数或复数标识符?
命名容器时,什么是更好的编码风格:
source = {}
#...
source[record] = some_file
或者
sources = {}
#...
sources[record] = some_file
复数在创建时读起来更自然;赋值时的单数。
这不是一个无聊的问题;当我不确定变量是容器还是单个值时,我确实发现自己对旧代码感到困惑。
更新
似乎有一个普遍的共识,即当字典用作映射时,最好使用更详细的名称(例如,recordToSourceFilename
);如果我绝对想使用短名称,则将其设为复数(例如,sources
)。
When naming a container , what's a better coding style:
source = {}
#...
source[record] = some_file
or
sources = {}
#...
sources[record] = some_file
The plural reads more natural at creation; the singular at assignment.
And it is not an idle question; I did catch myself getting confused in an old code when I wasn't sure if a variable was a container or a single value.
UPDATE
It seems there's a general agreement that when the dictionary is used as a mapping, it's better to use a more detailed name (e.g., recordToSourceFilename
); and if I absolutely want to use a short name, then make it plural (e.g., sources
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为字典有两个非常具体的用例,应该单独识别。然而,在解决它们之前,应该注意的是,字典的变量名几乎总是单数,而列表几乎总是复数。
作为类对象实体的字典: 有时您有一个代表某种类对象数据结构的字典。在这些情况下,字典几乎总是指单个类似对象的数据结构,因此应该是单数的。例如:
字典作为映射实体:其他时候,你的字典可能表现得更像一个典型的哈希映射。在这种情况下,最好使用更直接的名称,但仍然是单数。例如:
列表:最后,您有了列表,这是一个完全独立的想法。这些几乎总是复数,因为它们是其他实体的简单集合。例如:
我确信有一些晦涩或不太可能的情况可能需要在这里做出一些例外,但我认为这是命名字典和列表时需要遵循的一个非常强有力的指导原则,不仅在Python中,而且在Python中也是如此。所有语言。
I think that there are two very specific use cases with dictionaries that should be identified separately. However, before addressing them, it should be noted that the variable names for dictionaries should almost always be singular, while lists should almost always be plural.
Dictionaries as object-like entities: There are times when you have a dictionary that represents some kind of object-like data structure. In these instances, the dictionary almost always refers to a single object-like data structure, and should therefore be singular. For example:
Dictionaries as mapping entities: Other times, your dictionary might be behaving more like a typical hash-map. In such a case, it is best to use a more direct name, though still singular. For example:
Lists: Finally, you have lists, which are an entirely separate idea. These should almost always be plural, because they are simple a collection of other entities. For example:
I'm sure that there are some obscure or otherwise unlikely situations that might call for some exceptions to be made here, but I feel that this is a pretty strong guideline to follow when naming dictionaries and lists, not just in Python but in all languages.
它应该是复数,因为这样程序的行为就像您大声朗读一样。让我向您展示为什么它不应该是单数(完全人为的示例):
此外,有时最好有更明确的名称,您可以从中推断出映射(对于字典)以及可能的类型。我通常在引入字典变量时添加一个简单的注释。一个例子:
It should be plural because then the program behaves just like you read it aloud. Let me show you why it should not be singular (totally contrived example):
Furthermore, sometimes it's a good idea to have even more explicit names from which you can infer the mapping (for dictionaries) and probably the type. I usually add a simple comment when I introduce a dictionary variable. An example:
我更喜欢容器的复数形式。使用时有一定的可以理解的逻辑:
I prefer plurals for containers. There's just a certain understandable logic in using: