20

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

max
  • 49,282
  • 56
  • 208
  • 355

3 Answers3

21

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.

Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
  • The example in my question falls under (2). The full name is then `recordToSourceFile`. I can see how it's very clear. Would you ever use a shorthand `source` or `sources` in such a case? – max Feb 12 '12 at 20:18
  • I could see my self using `sources` in such a case, but only if the instance was particularly well documented or excruciatingly obvious. Shorthand is great, but being vague is not. :) – Patrick Perini Feb 12 '12 at 20:46
6

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")
AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • If you chose not to use a more explicit name `idToCustomer` for the dictionary, would you use the plural `customers`? – max Feb 12 '12 at 20:14
  • @max: Definitely, it just makes a lot more sense - as I tried to explain with my first examples. – AndiDog Feb 12 '12 at 22:11
5

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

entries = []
for entry in entries:
     #Code...
chrtan
  • 1,704
  • 1
  • 13
  • 17