Home >Backend Development >Python Tutorial >What is the Preferred Naming Convention for Python Variables and Functions?

What is the Preferred Naming Convention for Python Variables and Functions?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 03:47:13325browse

What is the Preferred Naming Convention for Python Variables and Functions?

Python's Naming Conventions for Variables and Functions

In Python, you're likely to encounter two naming conventions for variables and functions, just as you did in C#: CamelCase and PascalCase. However, there's an additional convention you may encounter in Python, known as snake_case.

So, which of these conventions is the preferred style for Python?

Python PEP 8 Guidelines

The answer lies in the Python Enhancement Proposal 8 (PEP 8) style guide. According to PEP 8:

For function names:

  • All lowercase characters should be used.
  • Words should be separated by underscores (_) to improve readability.

For variable names:

  • The same convention as function names applies.
  • mixedCase is allowed only in contexts where it's the established style (e.g., threading.py) for backward compatibility.

Example:

# snake_case
this_is_my_variable = 'a'
def this_is_my_function():

Therefore, snake_case is the preferred and definitive coding style for variables and functions in Python, as outlined in the official PEP 8 guidelines.

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

Statement:
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