Understanding Variable Naming Rules
The def keyword marks the start of a function definition and is required for every function.
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.
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.
| Pattern | Status | Reason |
|---|---|---|
| calculate_total | Valid | Starts with a letter and uses letters and an underscore |
| _calculate | Valid | Starts with an underscore |
| 2calculate | Invalid | Starts with a number |
| calculate total | Invalid | Contains a space |
| calculate-total | Invalid | Contains a hyphen |
| def | Invalid | Is 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.
| Style | Rule status | Python function convention |
|---|---|---|
| snake_case | Allowed by naming rules | Strongly preferred |
| camelCase | Allowed by naming rules | Not 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.
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
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?
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
- The def keyword begins every Python function definition.
- Function names follow the same rules as variable names: begin with a letter or underscore, then use only letters, numbers, and underscores.
- Function names cannot be Python keywords, begin with numbers, or contain spaces or hyphens.
- Python convention strongly favors snake_case for function names, even though camelCase is allowed by the naming rules.
- 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.