字典的单数或复数标识符?

发布于 2025-01-03 18:13:14 字数 414 浏览 3 评论 0原文

命名容器时,什么是更好的编码风格:

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 技术交流群。

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

发布评论

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

评论(3

段念尘 2025-01-10 18:13:14

我认为字典有两个非常具体的用例,应该单独识别。然而,在解决它们之前,应该注意的是,字典的变量名几乎总是单数,而列表几乎总是复数。

  1. 作为类对象实体的字典: 有时您有一个代表某种类对象数据结构的字典。在这些情况下,字典几乎总是指单个类似对象的数据结构,因此应该是单数的。例如:

     # 假设 users 是从某个 JSON 源解析的用户列表
     # 假设每个用户都是一个字典,包含该用户的信息
    
     对于用户中的用户:
         打印用户['姓名']
    
  2. 字典作为映射实体:其他时候,你的字典可能表现得更像一个典型的哈希映射。在这种情况下,最好使用更直接的名称,但仍然是单数。例如:

    # 假设 idToUser 是一个将 ID 映射到用户对象的字典
    
    用户 = idToUser['0001a']
    打印用户名
    
  3. 列表:最后,您有了列表,这是一个完全独立的想法。这些几乎总是复数,因为它们是其他实体的简单集合。例如:

    users = [userA, userB, userC] # 有意义
    对于用户中的用户:
        print user.name # 尤其是稍后,在迭代中
    

我确信有一些晦涩或不太可能的情况可能需要在这里做出一些例外,但我认为这是命名字典和列表时需要遵循的一个非常强有力的指导原则,不仅在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.

  1. 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:

     # assume that users is a list of users parsed from some JSON source
     # assume that each user is a dictionary, containing information about that user
    
     for user in users:
         print user['name']
    
  2. 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:

    # assume that idToUser is a dictionary mapping IDs to user objects
    
    user = idToUser['0001a']
    print user.name
    
  3. 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:

    users = [userA, userB, userC] # makes sense
    for user in users:
        print user.name           # especially later, in iteration
    

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.

你另情深 2025-01-10 18:13:14

它应该是复数,因为这样程序的行为就像您大声朗读一样。让我向您展示为什么它不应该是单数(完全人为的示例):

c = Customer(name = "Tony")
c.persist()

[...]

#
# 500 LOC later, you retrieve the customer list as a mapping from
# customer ID to Customer instance.
#

# Singular
customer = fetchCustomerList()
nameOfFirstCustomer = customer[0].name
for c in customer: # obviously it's totally confusing once you iterate
    ...

# Plural
customers = fetchCustomerList()
nameOfFirstCustomer = customers[0].name    
for customer in customers: # yeah, that makes sense!!
    ...

此外,有时最好有更明确的名称,您可以从中推断出映射(对于字典)以及可能的类型。我通常在引入字典变量时添加一个简单的注释。一个例子:

# Customer ID => Customer
idToCustomer = {}

[...]

idToCustomer[1] = Customer(name = "Tony")

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):

c = Customer(name = "Tony")
c.persist()

[...]

#
# 500 LOC later, you retrieve the customer list as a mapping from
# customer ID to Customer instance.
#

# Singular
customer = fetchCustomerList()
nameOfFirstCustomer = customer[0].name
for c in customer: # obviously it's totally confusing once you iterate
    ...

# Plural
customers = fetchCustomerList()
nameOfFirstCustomer = customers[0].name    
for customer in customers: # yeah, that makes sense!!
    ...

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:

# Customer ID => Customer
idToCustomer = {}

[...]

idToCustomer[1] = Customer(name = "Tony")
终难遇 2025-01-10 18:13:14

我更喜欢容器的复数形式。使用时有一定的可以理解的逻辑:

entries = []
for entry in entries:
     #Code...

I prefer plurals for containers. There's just a certain understandable logic in using:

entries = []
for entry in entries:
     #Code...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文