Skip to content

Python naming conventions

  • by

Python naming conventions refer to a set of guidelines and recommendations for naming variables, functions, classes, modules, and other elements in Python code. These conventions aim to improve code readability, maintainability, and consistency across different Python projects. The Python community widely follows these conventions to create clean and easily understandable code.

Here are the key points of Python naming conventions:

  1. Variables and Functions:
    • Use lowercase letters.
    • Separate words with underscores (snake_case).
    • Examples: my_variable, calculate_average()
  2. Constants:
    • Use uppercase letters.
    • Separate words with underscores (UPPERCASE_WITH_UNDERSCORE).
    • Examples: MAX_SIZE, DEFAULT_VALUE
  3. Modules and Packages:
    • Use lowercase letters.
    • Separate words with underscores (snake_case).
    • Examples: my_module, my_package
  4. Classes:
    • Use CamelCase (also known as PascalCase).
    • Start each word with an uppercase letter and don’t use underscores.
    • Examples: MyClass, MyAwesomeClass
  5. Private Members:
    • Prefix the name with a single underscore to indicate internal use.
    • Example: _internal_variable, _private_method()
  6. Protected Members:
    • Prefix the name with a single underscore to indicate internal use (can be accessed by subclasses).
    • Example: _protected_variable, _protected_method()
  7. Public Members:
    • No prefix is used.
    • Example: public_variable, public_method()
  8. Avoid Reserved Keywords:
    • Do not use Python’s reserved keywords (e.g., if, for, while, class, etc.) as names.

By following these conventions, your Python code will be more consistent, easier to read, and more readily understood by other developers. Adhering to naming conventions also enhances the collaboration and maintainability of Python projects.

Python naming conventions example

Here are some examples of Python naming conventions:

Variable and Function Names:

# Good:
name = "John Doe"
age = 25

def calculate_average(numbers):
    total = sum(numbers)
    average = total / len(numbers)
    return average

# Bad (not following conventions):
myName = "John Doe"
my_age = 25

def calcAvg(nums):
    totalSum = sum(nums)
    avg = totalSum / len(nums)
    return avg

Constant Names:

# Good:
MAX_SIZE = 100
DEFAULT_VALUE = 0

# Bad (not following conventions):
MaxSize = 100
default_value = 0

Module and Package Names:

# Good:
my_module.py
my_package/

# Bad (not following conventions):
MyModule.py
MyPackage/

Class Names:

# Good:
class MyClass:
    ...

class MyAwesomeClass:
    ...

# Bad (not following conventions):
class myClass:
    ...

class my_awesome_class:
    ...

Private Members:

# Good:
class MyClass:
    def __init__(self):
        self._internal_variable = 10

    def _private_method(self):
        ...

# Bad (not following conventions):
class MyClass:
    def __init__(self):
        self.internalVariable = 10

    def privateMethod(self):
        ...

Protected Members:

# Good:
class MyBaseClass:
    def __init__(self):
        self._protected_variable = 10

    def _protected_method(self):
        ...

# Bad (not following conventions):
class MyBaseClass:
    def __init__(self):
        self.protectedVariable = 10

    def protectedMethod(self):
        ...

Public Members:

# Good:
class MyClass:
    def __init__(self):
        self.public_variable = 10

    def public_method(self):
        ...

# Bad (not following conventions):
class MyClass:
    def __init__(self):
        self.publicVariable = 10

    def publicMethod(self):
        ...

Here’s a tabular format of Python naming conventions:

TypeConventionExample
Variablesnake_casemy_variable
Functionsnake_casecalculate_average()
ConstantUPPERCASE_WITH_UNDERSCOREMAX_SIZE, DEFAULT_VALUE
Module/Packagesnake_casemy_module, my_package
ClassCamelCaseMyClass, MyAwesomeClass
Private Member_underscore_prefix_internal_variable
Protected Member_underscore_prefix_protected_variable
Public MemberNo prefixpublic_variable

Do comment if you have any doubts or suggestions on this basic Python topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading