Python Naming Conventions


Like all programming languages, Python has conventions for naming. This post covers some of them for Python version 3.x. Following these conventions is recommended so that your code is easier to understand.

Class names should be upper case and all other names should be lower case.

Some words hold a special meaning in Python. You cannot use these words to name variables, functions, or classes. If you run import keyword, and then run print(keyword.kwlist) you get the following output: [‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’].

You can comment your code. This will help other programmers read and understand your code. It will also help you. You can use the pound sign (#) when describing code. Everything on the line after the # sign will be ignored by the interpreter. Don’t repeat your code in your comments. Do not comment every single line of code because the code explains itself to a knowledgable programmer. Your comments should be on a higher (more general) level and describe in plain language what the code is supposed to do.

Here is a link to a style guide called PEP 8 – Style Guide for Python Code. PEP stands for Python Enhancement Proposals. These are a running catalog of ways to improve or standardize Python as a language. It is many pages long. This document has been placed in the public domain.

Leave a Reply