Skip to content

Instantly share code, notes, and snippets.

@etigui
Last active July 2, 2024 18:25
Show Gist options
  • Save etigui/7600441926e73c3385057718c2fdef8e to your computer and use it in GitHub Desktop.
Save etigui/7600441926e73c3385057718c2fdef8e to your computer and use it in GitHub Desktop.
Python naming conventions

Python naming conventions

This document gives coding conventions example for the Python code. This style guide evolves over time as additional conventions are identified and past conventions are rendered obsolete by changes in the language itself.

1. General

  • Avoid using names that are too general or too wordy. Strike a good balance between the two.
    • Bad: data_structure, my_list, info_map, dictionary_for_the_purpose_of_storing_data_representing_word_definitions
    • Good: user_profile, menu_options, word_definitions
  • Never use the characters as single character variable names:
    • “l“ : lowercase letter el
    • “O“ : uppercase letter oh
    • “I“ : uppercase letter eye
  • When using CamelCase names, capitalize all letters of an abbreviation (e.g. HTTPServer)

2. Packages

  • Package names should be all lower case
  • When multiple words are needed, an underscore should separate them
  • It is usually preferable to stick to 1 word names

Example:

pyramid
[or]
pyramid_giza

3. Modules

  • Module names should be all lower case
  • When multiple words are needed, an underscore should separate them
  • It is usually preferable to stick to 1 word names

Example:

pyramid.py
[or]
pyramid_giza.py

4. Classes

  • Class names should follow the UpperCaseCamelCase convention
  • Python's built-in classes, however are typically lowercase words
  • Exception classes should end with (suffix) “Exception”

Example:

    class PyramidGiza():
    class InputException(): # Exception

5. Global (module-level) Variables

  • Global variables should be all lowercase
  • Words in a global variable name should be separated by an underscore
  • It is preferable to use these variables inside one module only

Example:

    pyramid_giza = "pyramid of giza"

6. Instance Variables

  • Instance variable names should be all lower case
  • Words in an instance variable name should be separated by an underscore
  • Protected instance variables should begin with a single underscore
  • Private instance variables should begin with two underscores

Example:

    pyramid_giza = "pyramid of giza" # Public
    _pyramid_giza = "pyramid of giza" # Protected
    __pyramid_giza = "pyramid of giza" # Private

7. Methods

  • Method names should be all lower case
  • Words in an method name should be separated by an underscore
  • Protected method should begin with a single underscore
  • Private method should begin with two underscores underscore

Example:

    def pyramid_giza(): # Public
    def _pyramid_giza(): # Protected
    def __pyramid_giza(): # Private

8. Method Arguments

  • Instance methods should have their first argument named "self"
  • Class methods should have their first argument named "cls"
  • Static method is similar to a class method but, won't get the class object

Example:

    class PyramidGiza(object):
        def instance_method(self):
            print(f'Hello from {self.__class__.__name__}')

        @classmethod
        def class_method(cls):
            print(f'Hello from {cls.__name__}')

        @staticmethod
        def static_method():
            print(f'Hello from {PyramidGiza.static_method.__name__}')

9. Functions

  • Function names should be all lower case
  • Words in a function name should be separated by an underscore

Example:

    def pyramid_giza():

10. Constants

  • Constant names must be fully capitalized
  • Words in a constant name should be separated by an underscore

Example:

TOTAL
[or]
MAX_CAPACITY

Ref

@h4k1m0u
Copy link

h4k1m0u commented Jun 11, 2021

I think there's a typo in your explanation for private/protected methods, as it doesn't match the example given just below (in terms of number of underscores). You say:

Private method should begin with a single underscore

Yet in the code you declare a private method with two underscores.

@etigui
Copy link
Author

etigui commented Jan 7, 2022

Sorry for the lat replay. Indeed it was typo, I just update it. Thanks ! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment