Search code examples
pythonnaming-conventionspep8

Python distinguish between similar variable names (naming convention)


I'm new to Python. I've read somewhere the best practice to distinguish between similar variable names, but now I can no longer find the page. I thought it was in PEP8, but I can't find it there.

As I recall it was better to choose the position of the adjective, for example, car_red and car_blue over red_car and blue_car. Isn't something to that effect in a naming convention document?

Thanks, Chris


Solution

  • I don't think so, also I don't think convention rules should go to that point. For Python you have PEP8. If everyone follows it, source code from different developers becomes visually more regular and that's a good thing for colaboration, reviews and code maintenance.

    Of course there are multiple sources of recommendations of that type, sometimes with variations depending on the target language.

    Searching for "variable naming best practices" gives you pointers. For example here for Java.

    Use common-sense for the situations at hand, instead of applying some of the recomendations blindly (well you are also right if you think that conventions were made because common-sense varies widely). What I mean is that some choices are subjective, or different choices can still be good choices. Just try to avoid bad choices.

    About your specific example, you are naming a thing, so putting what the thing is first (the noun) makes the reader know right away what the var is about. Adding an attribute can be a bad choice because usually attributes can change later, and so they may not define properly what the thing is. But adding new, copy, saved, temp, aux, first, last, active, disabled, running, sold, count are useful examples.