Following PEP 8 Style Guidelines
The main style difference is convention: Python code normally uses snake_case for function names.
A Function Name Is an Identifier
Function names can look different from variable names because functions perform actions, but Python does not give them a separate naming system. A function name is an identifier, so it follows the same rules as a variable name. The main style difference is convention: Python code normally uses snake_case for function names.
A valid function name starts with a letter or underscore, can then contain letters, numbers, and underscores, and cannot be a Python keyword.
Reading a Function Definition
The def keyword marks the start of a function definition and is required for every function.
When Python sees def, it expects a function name, parentheses for parameters, a colon, and a function body. In the example, calculate_total is the function name. Its spelling follows snake_case, and the name itself follows the same identifier rules as a variable name.
Naming Rules in Practice
Checking possible function names
Decide which names follow Python's function naming rules and which names should be avoided.
- 1calculate_total
This name starts with a letter and uses only letters and an underscore. It also follows the preferred
snake_caseconvention. - 2_read_file
This name starts with an underscore, which is allowed. It uses no invalid characters.
- 32calculate
This name begins with a number, so it does not follow the naming rules.
- 4calculate-total
The hyphen is not one of the permitted characters for a name.
- 5def
def is a reserved keyword used to begin a function definition, so it cannot be used as a function name.
- 6calculateTotal
This spelling can satisfy the basic identifier character rules, but Python convention strongly favors
calculate_totalbecause function names should usesnake_caserather thancamelCase.
Valid naming rules and style conventions are related but not identical. A name must first be legal Python syntax, and it should then follow the snake_case convention used by Python code.
| Question | Rule | Example |
|---|---|---|
| What may begin the name? | A letter or underscore | read_data, _read_data |
| What may appear after the first character? | Letters, numbers, and underscores | read2_data |
| Can a keyword be used? | No | def is not allowed |
| Which style is preferred? | snake_case | read_data |
Function names use the same validity rules as variable names; snake_case is the preferred Python convention.
When Names Collide
A name in a scope should not be used for both a function and a variable. If a function is defined with a name and a later variable assignment uses that same name, the variable assignment overwrites the function associated with that name. The function then becomes inaccessible through that name.
At first, report_total is the name of the function defined by def. The later assignment gives report_total a variable value instead. The important point is not that the function has been renamed; it is that the name no longer provides access to the function. Using distinct names prevents this accidental overwrite.
Mistakes with Function Names
Common mistake Assuming function names have different rules from variable names
What it looks likedef 2process_data():Why it failsFunction names follow the same identifier rules as variable names, including the rule that a name cannot begin with a number.
Do this insteadUse a name such as process_data.Common mistake Using spaces or punctuation inside a function name
What it looks likedef process-data():Why it failsFunction names may contain only letters, numbers, and underscores after beginning with a letter or underscore.
Do this insteadUse process_data.Common mistake Using a Python keyword as the function name
What it looks likedef def():Why it failsdef is reserved for starting a function definition.
Do this insteadChoose a different identifier, such as define_result.
Common mistake Using camelCase when following Python convention
What it looks likedef calculateTotal():Why it failsThe name may satisfy basic character rules, but PEP 8 convention strongly favors
snake_casefor function names.Do this insteadUse calculate_total.Common mistake Reusing a function name for a variable
What it looks likedef get_score(): return 10 get_score = 10Why it failsThe variable assignment overwrites the function associated with that name in the same scope.
Do this insteadGive the variable a distinct name, such as score.
Practice the Rules
For each proposed function name, decide whether it is valid and whether it follows the preferred Python naming convention: save_results, 7_days, save-results, class, and saveResults. Then rewrite every unsuitable name that can be rewritten.
Hints
- Check the first character.
- Check every character that follows it.
- Check whether the name is a Python keyword.
- Separate basic validity from the
snake_casestyle convention.
What do you think happens?
After the following statements, what does the name display_score refer to?
Reveal answer
The variable value
The later variable assignment uses the same name and overwrites the function associated with that name in the same scope. Distinct names should be used for the function and the variable.
Rules to Carry Forward
- 1Every function definition begins with the def keyword.
- 2Function names follow the same validity rules as variable names: begin with a letter or underscore, then use only letters, numbers, and underscores, and avoid keywords.
- 3Python convention strongly favors
snake_casefor function names. - 4Do not use the same name for a function and a variable in the same scope because the variable assignment overwrites access to the function.
- 5A legal identifier and a clear, conventional identifier are not always the same: check both syntax and style.
Key Takeaways
- The def keyword marks the beginning of every Python function definition.
- Function names use exactly the same naming rules as variable names.
- Names must start with a letter or underscore, use only letters, numbers, and underscores, and avoid Python keywords.
- PEP 8 convention favors
snake_caserather thancamelCasefor function names. - Assigning a variable the same name as a function overwrites the function's name binding in that scope.