Home > Blockchain >  Proper naming conventions for a dictionary in Python
Proper naming conventions for a dictionary in Python

Time:04-06

After having someone read through my code, I noticed they got confused by the name of a variable containing a dictionary. The variable was named after the purpose of the keys in the dictionary, not the values, which made them confused. This made me wonder if the "correct" way to name dictionaries is after the keys, values or both?

CodePudding user response:

I think the following link might help you: Python Naming Conventions

In a nutshell: there is no convention, just give your variables proper names so that when people read through your code, they know what its purpose is.

CodePudding user response:

Maybe share the dictionary to make the question a bit clearer?

I'm not convinced there is a "correct" way to do this (or that that idea really applies to programming in general). Yes, Python has this idea of There should be one--and preferably only one--obvious way to do it., but that doesn't hold for a lot of things.

I'd probably lean towards keys, but the question is complicated by the fact that dictionaries can contain non-uniform data, so the keys might not represent the same sorts of things.

Your best bet is to aim to make your code easy to read and understand—give things names that describe what they are/contain, name functions for what they do, et cetera. The idea of self-documenting code is useful here.

  • Related