views:

105

answers:

3

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.

+1  A: 

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.

Ignacio Vazquez-Abrams
+1 I like this naming convention because it makes it known very quickly that you are dealing with a dictionary and not a list. The only thing I don't like is that the entire word map seems like overkill, is there a popular standard that accomplishes this with some sort of shorthand for 'map'? Is there a Pythonic way to remind the code reviewer that the object is a map?
pokstad
+2  A: 

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 value_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".

viraptor
+1 value_key_map is straight to the point and very readable.
pokstad
+1  A: 

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.

unutbu
That was my first instinct, but I feel like the naming convention between my lists and my dictionaries are too similar. When I come back to revisit old code I constantly have to find where something is initialized or accessed by key so that I know my dictionary is not a list or vice versa.
pokstad
@pokstad: Yes, I see what you're saying. However, if you look at the standard library, its dicts seem to be named after the `values`only, never the more wordy `values_key_map`. For example, `sys.modules`, or `locals()`. That seems to be the way at least the developers think is best. Perhaps a bit of documentation could help too.
unutbu
+1 Thanks for the concrete reference.
pokstad