Skip to main content

Python Naming Conventions

Python Naming Conventions

This really helped me out a lot when pylint would output ambiguous error messages that wouldn't really help me to solve the "riddle". For instance incorrect function name, what the heck does that mean to someone who has never used python or at least not that often? Everything that I could find in Google, the forums and even pylint's web page, wouldn't really give me the answers to my syntax naming convention woes. Hope this helps someone else.

Many thanks to the people over at CodingConvention on GitHub

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
  • Don’t be a jackass and name things “O”, “l”, or “I”
  • 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

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

4. Classes

  • Class names should follow the UpperCaseCamelCase convention
  • Python’s built-in classes, however are typically lowercase words
  • Exception classes should end in “Error”

5. Global (module-level) Variables

  • Global variables should be all lowercase
  • Words in a global variable name should be separated by an underscore

6. Instance Variables

  • Instance variable names should be all lower case
  • Words in an instance variable name should be separated by an underscore
  • Non-public instance variables should begin with a single underscore
  • If an instance name needs to be mangled, two underscores may begin its name

7. Methods

  • Method names should be all lower case
  • Words in an method name should be separated by an underscore
  • Non-public method should begin with a single underscore
  • If a method name needs to be mangled, two underscores may begin its name

8. Method Arguments

  • Instance methods should have their first argument named ‘self’.
  • Class methods should have their first argument named ‘cls’

9. Functions

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

10. Constants

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

Source

CodingConvention on GitHub

Comments

Popular posts from this blog

Emby Media Server | Arch Linux

Prerequisite:I have tried to install Emby directly from the Arch package repositories, but for whatever reason, permissions for the emby service user were not allowing emby to access the media file locations. Now that I have used it a little bit, Docker seems to be the best, and easiest way, to get Emby up and running reliably. Below is my experience running through the emby installation process. Most of it comes from the GitHub tutorial, but I've added my take and what I've done to resolve certain issues.Note: The following install was done on an Arch Linux box, running the most recent kernel, which at the time was 4.3.3.-2-ARCH.You can get he full GitHub installation guide from herePrerequisite:First, make sure that you have a working Docker instance:sudo pacman -S docker Now, start the docker service:sudo systemctl enable docker sudo systemctl start docker Your docker container should now be running, and it should startup after each reboot of your system. To Check that the …

Installing Arch Linux & Gnome 3 Desktop

Installation:The following steps are what I did to install Arch linux on my Asus laptop and many other devices. The Beginners Install Guide is much more in-depth and explains some other features like MBR installs, full and partial disk encryption, and the GRUB boot loader.Installation:Bootable Arch Media:You can grab the latest ISO from here. I always grab one from one of the US mirrors, but they have a Torrent file as well.Now lets create the install media:# sudo dd bs=512 if=/location/of/your/iso_file of=/dev/sdx && sync This will take about 5 minutes to complete, but it will depend on the quality of the flash media.Install Arch:Boot to the USB drive that you just created, and select the first boot option.After the OS environment boots, take a look at the connected devices and determine where you will be installing Arch. If you know how large your target drive is, determining which device to use should be fairly straight forward.# lsblk Network Setup:If you are connect to yo…