Home > Backend Development > Python Tutorial > What are the Recommended Naming Conventions for Variables and Functions in Python?

What are the Recommended Naming Conventions for Variables and Functions in Python?

Linda Hamilton
Release: 2024-11-27 15:26:10
Original
947 people have browsed it

What are the Recommended Naming Conventions for Variables and Functions in Python?

Naming Conventions in Python: Variables and Functions

In Python, unlike C# where camelCase or PascalCase is prevalent, a different naming convention is observed. Let's explore the preferred practices for Python.

CamelCase and PascalCase in Python

Although you may encounter camelCase and PascalCase in Python, these conventions are generally not recommended.

Snake Case

The preferred naming convention in Python is snake_case, where words are separated by underscores.

Example:

this_is_my_variable = 'a'
def this_is_my_function():
Copy after login

PEP 8 Guidelines

The Python Enhancement Proposal 8 (PEP 8) outlines the coding style conventions for Python. It explicitly states that:

  • Function names should be lowercase, separated by underscores for readability.
  • Variable names follow the same convention as function names.
  • mixedCase is acceptable only in specific contexts where it is already common practice to preserve backward compatibility.

Additional Considerations

  • Class names use the convention CapitalizedWords (e.g., MyClass).
  • Constants use the convention UPPER_CASE_WITH_UNDERSCORES.
  • Avoid using special characters (e.g., $, %, ^) in variable names.
  • Keep variable names short and descriptive.

The above is the detailed content of What are the Recommended Naming Conventions for Variables and Functions in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template