- etigui / python_naming_conventions.md
- Naming Convention in Python Programming
- What is Naming Conventions
- Naming Conventions rules for Variables, Packages, Modules and Functions (Methods) are:
- Example 1
- Example 2
- Example 3
- Example 4
- Example 5
- Example 6
- Naming Conventions rules for Classes are:
- Example
- Test Your Knowledge
etigui / python_naming_conventions.md
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.
- 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
- “l“ : lowercase letter el
- “O“ : uppercase letter oh
- “I“ : uppercase letter eye
- 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
- 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
pyramid.py [or] pyramid_giza.py
- Class names should follow the UpperCaseCamelCase convention
- Python’s built-in classes, however are typically lowercase words
- Exception classes should end with (suffix) “Exception”
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
pyramid_giza = "pyramid of giza"
- 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
pyramid_giza = "pyramid of giza" # Public _pyramid_giza = "pyramid of giza" # Protected __pyramid_giza = "pyramid of giza" # Private
- 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
def pyramid_giza(): # Public def _pyramid_giza(): # Protected def __pyramid_giza(): # Private
- 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
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__>')
- Function names should be all lower case
- Words in a function name should be separated by an underscore
- Constant names must be fully capitalized
- Words in a constant name should be separated by an underscore
Naming Convention in Python Programming
In this lesson, we will learn about the Naming Convention in the Python programming language. We will go through the rules of naming Variables, Packages, Modules, Functions, and Classes, along with some examples and a quiz on it.
What is Naming Conventions
In Python programming, naming conventions are set of rules for choosing the valid name to be used for variable, package, module, class and function in a python program.
Naming Conventions rules for Variables, Packages, Modules and Functions (Methods) are:
- It should begin with an alphabet.
- It should be written in lowercase.
- There may be more than one alphabet, but without any spaces between them.
- Digits may be used but only after alphabet.
- No special symbol can be used except the underscore (_) symbol. When multiple words are needed, an underscore should separate them.
- No keywords or command can be used.
- All statements in Python language are case sensitive. Thus a name A (in uppercase) is considered different from a name a (in lowercase).
Now let’s see some examples for more understanding.
Example 1
It should begin with an alphabet.
x # x is a valid variable name because it starts with an alphabet x
Example 2
There may be more than one alphabet, but without any spaces between them.
total # total is a valid variable name as there is no space between alphabets
Example 3
Digits may be used but only after alphabet.
ar15 # ar15 is a valid variable name as digits have been used after the alphabet a6b2 # a6b2 is a valid variable name as digits have been used after the alphabet
Example 4
No special symbol should be present within the variable name except underscore _.
total_cost # total_cost is a valid variable name as there is an underscore total cost # total cost is an invalid variable name as there is a space total-cost # total-cost is an invalid variable name as there is a hyphen total$ # total$ is an invalid variable name as there is a dollar symbol
Example 5
No keywords or command can be used as a variable name.
for # here for is an invalid variable name because it is a keyword in Python if # if is an invalid variable name because it is a keyword in Python elif # elif is an invalid variable name because it is a keyword in Python while # while is an invalid variable name because it is a keyword in Python
Example 6
All statements in Python language are case sensitive. Thus a variable A (in uppercase) is considered different from a variable declared a (in lowercase).
a # a is a valid variable written in lowercase A # A is a valid variable written in uppercase so both are different variables
Naming Conventions rules for Classes are:
- It should begin with an uppercase letter.
- There may be more than one alphabet, but without any spaces between them.
- No special symbol can be used.
- No keywords or command can be used as a class or interface name.
- If the class name is more than one word then the first letter of each word should be written in uppercase and the rest of the letter should be written in lowercase.
Example
Game # it is a valid 1 word class name EmployeeInfo # it is a valid 2 words class name TotalCostPrice # it is a valid 3 words class name ServerNotActiveException # it is a valid 4 words class name
Note: Python’s built-in classes are typically lowercase words. But user defined classes should follow the above naming rules.
Test Your Knowledge
Attempt the multiple choice quiz to check if the lesson is adequately clear to you.