Concepts / Following PEP 8 Style Guidelines

Following PEP 8 Style Guidelines

The def keyword marks the start of a function definition and is required for every function.

  • Programming

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.

python

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.

calculate_total: This name starts with a letter and uses only letters and an underscore. It also follows the preferred snake_case convention.

_read_file: This name starts with an underscore, which is allowed. It uses no invalid characters.

2calculate: This name begins with a number, so it does not follow the naming rules.

calculate-total: The hyphen is not one of the permitted characters for a name.

def: def is a reserved keyword used to begin a function definition, so it cannot be used as a function name.

calculateTotal: This spelling can satisfy the basic identifier character rules, but Python convention strongly favors calculate_total because function names should use snake_case rather than camelCase.

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.

QuestionRuleExample
What may begin the name?A letter or underscoreread_data, _read_data
What may appear after the first character?Letters, numbers, and underscoresread2_data
Can a keyword be used?Nodef is not allowed
Which style is preferred?snake_caseread_data

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.

refers torefers toreport_totalfunctionfunction definitionreport_totalvariable valuelater assignment
What changes when a variable is assigned the same name as an existing function?
python

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

  • Assuming function names have different rules from variable names

    Function names follow the same identifier rules as variable names, including the rule that a name cannot begin with a number.

    Fix: Use a name such as process_data.

  • Using spaces or punctuation inside a function name

    Function names may contain only letters, numbers, and underscores after beginning with a letter or underscore.

    Fix: Use process_data.

  • Using a Python keyword as the function name

    def is reserved for starting a function definition.

    Fix: Choose a different identifier, such as define_result.

  • Using camelCase when following Python convention

    The name may satisfy basic character rules, but PEP 8 convention strongly favors snake_case for function names.

    Fix: Use calculate_total.

  • Reusing a function name for a variable

    The variable assignment overwrites the function associated with that name in the same scope.

    Fix: Give the variable a distinct name, such as score.

Practice the Rules

MEDIUM

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_case style convention.

What do you think happens?

After the following statements, what does the name display_score refer to?

  • The function
  • The variable value
  • Both at the same time
Reveal answer

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

  1. Every function definition begins with the def keyword.
  2. Function 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.
  3. Python convention strongly favors snake_case for function names.
  4. Do not use the same name for a function and a variable in the same scope because the variable assignment overwrites access to the function.
  5. A 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_case rather than camelCase for function names.
  • Assigning a variable the same name as a function overwrites the function's name binding in that scope.