Concepts / Understanding Variable Naming Rules

Understanding Variable Naming Rules

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

  • Programming

Why Names Matter

A function name may look different from a variable name because it describes an action, but Python applies the same naming rules to both. The name must be a valid identifier, and it must not conflict with Python's reserved keywords. Understanding this prevents syntax errors and helps you avoid accidentally replacing a function with a variable.

Function names do not have a separate naming system. They follow the exact same rules as variable names.

Reading a Function Definition

Every Python function definition begins with the def keyword. This keyword tells Python that a function is being defined. After def, Python expects a function name, parentheses for parameters, a colon, and then the function body. The function name is therefore an identifier, just like a variable name.

followed byfollowed byfollowed byintroducesdefstarts definitionfunction_namefunction identifier()parameters:expects bodyfunction bodyinstructions
Where does def appear, and how does it connect the function name, parameters, and body?

Tracing a Function Name

Determine which parts of a function definition are controlled by naming rules.

Find def: The def keyword marks the beginning of the function definition. It is required for every function and cannot be used as a function name or variable name.

Check the name: The word immediately after def is the function name. Because it is an identifier, it must follow the same rules as a variable name.

Check the remaining structure: Python then expects parentheses for parameters, a colon, and the function body.

The function name is not special: it is an identifier placed after def and must obey ordinary variable naming rules.

Valid Names and Invalid Patterns

A valid function name starts with a letter or an underscore. After the first character, it can use letters, numbers, and underscores. It cannot contain spaces or hyphens, and it cannot be a Python keyword. These restrictions give Python a clear way to recognize names and distinguish them from other parts of the language.

same valid categorysame invalid categorysame invalid categorysame invalid categorycalculate_totalvalid2calculatenumber starts name_calculatevalidcalculate totalcontains spacecalculate-totalcontains hyphendefreserved keyword
Which names follow Python's identifier rules, and what makes the other patterns invalid?
PatternStatusReason
calculate_totalValidStarts with a letter and uses letters and an underscore
_calculateValidStarts with an underscore
2calculateInvalidStarts with a number
calculate totalInvalidContains a space
calculate-totalInvalidContains a hyphen
defInvalidIs a reserved Python keyword

Generated naming examples applying the rules described in the source.

Choosing the Pythonic Style

Python's naming rules allow both snake_case and camelCase, but Python convention strongly favors snake_case for function names. In snake_case, words are separated by underscores, as in calculate_total. Following this convention makes function names consistent with broader Python code and easier for other programmers to read.

StyleRule statusPython function convention
snake_caseAllowed by naming rulesStrongly preferred
camelCaseAllowed by naming rulesNot the preferred convention

When a function name contains multiple words, separate the words with underscores and use lowercase letters, following snake_case. Treat the naming rule and the style convention as two separate checks: first ask whether the name is valid, then ask whether it follows Python's preferred style.

When Names Collide

A function name and a variable name should not be reused in the same scope. If a variable is assigned the same name as an existing function, the variable assignment overwrites the function reference. The function then becomes inaccessible through that name. This can create confusing behavior because the original function definition may look correct, while a later assignment has changed what the name refers to.

later assignment overwrites referenceprocess_datafunctionprocess_datavariable
What changes when a variable is assigned the same name as an existing function?

Tracing a Reused Name

A function is given the name process_data, and later a variable is given that same name. What should you expect?

Initial binding: At first, the name process_data refers to the function.

Variable assignment: A later assignment uses process_data as a variable name in the same scope.

Changed reference: The assignment overwrites the function reference associated with that name.

The name process_data now refers to the variable rather than the function, so the function is inaccessible through that name.

Naming Practice

MEDIUM

Classify each proposed function name as valid or invalid, then explain why: send_message, 3rd_place, send-message, def, and sendMessage. For every valid name, decide whether it follows Python's preferred snake_case convention.

Hints
  • Check the first character before checking the remaining characters.
  • Look for spaces, hyphens, and reserved keywords.
  • A name can be valid under Python's naming rules without being the preferred Python style.

What do you think happens?

What happens to a function when a variable in the same scope is assigned the function's name?

  • The function keeps the name and the variable receives a different automatic name
  • The variable assignment overwrites the function reference
  • Python changes the function name to snake_case
  • Both remain accessible through the same name
Reveal answer

Answer: The variable assignment overwrites the function reference.

A function name and a variable name refer to the same name in that scope. Assigning the variable name again changes what the name refers to, making the function inaccessible through that name.

Key Takeaways

  1. The def keyword begins every Python function definition.
  2. Function names follow the same rules as variable names: begin with a letter or underscore, then use only letters, numbers, and underscores.
  3. Function names cannot be Python keywords, begin with numbers, or contain spaces or hyphens.
  4. Python convention strongly favors snake_case for function names, even though camelCase is allowed by the naming rules.
  5. Using the same name for a function and a variable in the same scope lets the variable assignment overwrite the function reference.

Key Takeaways

  • The def keyword marks the beginning of a function definition.
  • Function identifiers obey exactly the same naming rules as variable identifiers.
  • Numbers cannot begin a name, and spaces, hyphens, and reserved keywords cannot be used in names.
  • snake_case is Python's preferred function naming convention.
  • Reusing a function name for a variable in the same scope overwrites the function reference.