Main Page | Recent changes | Edit this page | Page history

Printable version | Disclaimers | Privacy policy

Not logged in
Log in | Help
 

Style:Names

From 6.00 reference wiki

Elements of Style for Python Programmers: Main | Names | Variable Declarations | Indentation | Exceptions


The names of variables and functions should be long enough to be descriptive and distinct.

In general, names that might be used anywhere in a program – like functions -- should be self-describing, so that when you see the name you can understand what it means without having to go find the function definition. So factorial is a better function name than fact, and f would be far too short.

But don’t make names unnecessarily long. computeFactorial and factorialFunction are redundant compared to factorial. Long names are slower to type and slower to read.

Variables should also be self-describing, but since most variables are local (used inside one function and nowhere else), they don’t need to be as long. Sometimes variables may be only single characters. There are some common conventions for single-letter variables: n for an integer; i, j,k for integers used in loops; s and t for strings; x, y, z for real numbers or coordinates. Don’t use a single-letter name unless your variable falls into one of these categories. Use a more descriptive name.

     # GOOD: descriptive function name, and conventional single-letter variable name
     def factorial(n):
         if n == 0:
             return 1
         else:
             return n*factorial(n-1) 

Avoid lowercase "L" for single-letter variable names, because in many fonts it’s nearly indistinguishable from the number 1:

     l = l + 1   # BAD: which is the variable, and which is the number? 

Don’t use the variable name temp (which means "temporary"). Every local variable is temporary, after all. Use something more descriptive.

Don’t number your variables unless they’re related.

     # BAD: lazy variable naming
     x1 = "Homer Simpson"
     x2 = "743 Evergreen Terrace"
     # GOOD: descriptive names
     name = "Homer Simpson"
     addr = "743 Evergreen Terrace"

Use underscores or capital letters to separate the words in a name.

Many good descriptive names require more than one word, like "movie title" or "birthday probability." Since you can’t use spaces in a variable or function name, you have to find some other way to make them readable. Don’t just write movietitle or birthdayprobability.

Python programs generally use underscores to separate the words: movie_title. Programs in other languages use a convention where a capital letter to start each word: movieTitle. This mixed-case convention is often called 'camel case', because it looks like it has humps.

You can use either approach. Underscores are easier to read but slower to type. Camel case is easier to type but can be a little risky if you’re not consistent about capitalization. Remember that Python names are case-sensitive. If a name contains an acronym in it, like OK or HTTP, keep the acronym as all caps: inputOK, not inputOk.

Whichever convention you use, be consistent. As far as Python is concerned, the names input_ok, inputOK and inputOk are all different, but a human reader can easily confuse them.

Constants should be all capital letters.

To distinguish named constants – which always have the same value – from variables and functions, put the constants in all caps: e.g. PI, MAX_PROBABILITY, NUM_ITERATIONS. Obviously, camel case wouldn’t be useful for separating words in constant names, so you have to use underscores here.

Retrieved from "http://slice.csail.mit.edu../../../n/a/m/Style%7ENames_7a16.html"

This page has been accessed 65 times. This page was last modified 11:40, 6 February 2006 by 6.00 reference wiki user Asfandyar.


[Main Page]
Main Page
Recent changes

Edit this page
Discuss this page
Page history
What links here
Related changes

Special pages
Bug reports