Skip to content

Instantly share code, notes, and snippets.

@shenoy-anurag
Last active January 28, 2020 12:58
Show Gist options
  • Save shenoy-anurag/15d2e162670b16ae869e10fbe29b20c8 to your computer and use it in GitHub Desktop.
Save shenoy-anurag/15d2e162670b16ae869e10fbe29b20c8 to your computer and use it in GitHub Desktop.
Python Naming Conventions

Python Naming Conventions:

Classes:

  • Case: PascalCase
  • Keep it short and descriptive. Preferably a noun e.g. Car, Bird, MountainBike. Avoid acronyms and abbreviations.
  • Eg: OrderNumberGenerator() or Order()

Instance Variables:

  • Case: snake_case/lower_with_under
  • eg: self.items_list = []

Private Variables/Functions:

  • Case: snake_case/lower_with_under with initial underscore
  • eg: _failure_count = 0

Global Variables/Class Constants:

  • Case: CAPS_WITH_UNDER aka SCREAMING_SNAKE_CASE
  • Should be all uppercase letters and if the name contains multiple words, it should be separated by underscores (_)
  • eg: Global: ACTIONS_COLLECTION, Class Constant: RANDOM_SEED = 42

Variables:

  • Case: snake_case
  • keep it descriptive -- may be long. Don't use camelCase or PascalCase.
  • eg: order_id = 1

Boolean Variables (Variables):

  • Case: snake_case
  • Use names that ask questions (Yes or No)
  • eg: is_valid = False

Lists (Variables):

  • Case: snake_case
  • Use names that are plural to signify a collection of items or append "_list"
  • eg: names = [] or items_list = []

Dictionaries (Variables):

  • Case: snake_case

  • Use "natural naming" for dictionaries that don't fit the subtypes i.e. name it based on the values in the dict. or append "_dict"

  • eg: profile_details = {"first": "Albert", "last": "Einstein"} or users_dict = {"user1": {"name": "user1", "email": "abc@example.com"}}

    Subtypes:

     Maps:
     - These are mappings of a set of objects from key to value.
     - Use "key_to_value" or "value_key_map"
     - eg: "key_to_value": oranges_to_apples = {"orange1": "apple1", "orange2": "apple2"}
     - eg: "value_key_map": days_api_hits_map = {"0": 100, "1": 200, "3": 90}
     - Tips: Use whichever suits the data. days_to_api_hits might be understood as an integer countdown to a certain api_hits value.
    

Methods/Functions:

  • Case: snake_case/lower_with_under
  • Preferably a verb e.g. get_car(), purchase(), book()
  • eg: create_random_number()

DATABASE:

MONGODB:

  • Use the naming conventions of the programming language used.
Collections:
  • Case: snake_case
  • Keep it short and descriptive. If your collection holds user details, name it "users" or "users_collection".
  • eg: "products", "actions_collection", "addresses", "configurations".
Documents:
  • Case: snake_case
  • Keep it descriptive.
  • eg: In users collection, each document might have: {"email": "", "password": "", "last_login": "", "name": ""}

POSTGRES (SQL):

  • Use the naming conventions of the programming language used. Preferably use Python Naming Conventions as it makes writing CRUD scripts easier (you can access the table columns directly without needing to put column names in quotes).
Tables:
  • Case: snake_case
  • Short, decriptive and plural.
  • eg: "orders" for orders table, "products" for products, "users" for user details etc.
Columns:
  • Case: snake_case
  • Short, decriptive and singular.
  • eg: "order_id" for order id number, "first_name", "last_name" of user etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment