Published on

Python Identifiers - Unlocking the Secrets of Naming Conventions

Authors

Introduction

At its core, an identifier in Python is a name given to entities like variables, functions, classes, modules, or objects. These names serve as labels, allowing us to access and manipulate the associated entity. However, not all names can be identifiers, as Python enforces certain rules and conventions.

A python identifier is a name used to identify a variable, function, class, module or other objects.

An Identifier starts with a letter A-Z or a-z or an underscore( _ ) followed by zero or more letters, underscore & digits (0 to 9).

The following are naming conventions for Python Identifiers:

  • Class names start with an uppercase letter. All other identifiers start with a lowercase letter and these identifier are called as public identifier.

  • Starting an Identifier with a single leading underscore indicates that the identifier is protected

  • Starting an Identifier with two leading underscore indicating a strongly private identifiers.

  • If the Identifier starts and ends with two trailing underscores the identifier is a language-defined special name.

Descriptive Names

Choose descriptive names that convey the purpose of the identifier. For example, instead of 'x' or 'temp,' use 'temperature' or 'counter' for better clarity.

Camel Case

For variables and functions, use camel case, where the first letter is lowercase, and subsequent words start with uppercase letters. For instance, 'myVariable' or 'calculateInterestRate()'.

Snake Case

For naming functions, variables, or module names, use snake case, where words are separated by underscores. For example, 'user_age' or 'math_operations.py'.

UPPERCASE

Constants should be named in all uppercase letters, separated by underscores. For instance, 'MAX_VALUE' or 'PI'.

Avoid Single-Character Names

Avoid using single-letter names like 'x' or 'y' except for simple loop variables.

Public, Protected and Private Attributes

  • Public attributes can be accessed anywhere inside or outside of the class definition.

  • Protected (restricted) attributes should only be used under certain conditions.

  • Private attributes can only be accessed inside of the class definition.

NamingTypeMeaning
namePublicThese attributes can be freely used inside or outside of a class definition
_nameProtectedProtected attributes should be used outside of the class definition, unless inside of a subclass definition
__namePrivateThis kind of attribute is inaccessible and invisible. It's neither possible to read nor to write those attributes, except inside of the class definition itself

Also, the str.isidentifier() function will tell us if a string is a valid identifier. This is available since Python 3.0.

main.py

  '__99__'.isidentifier()

  # Output: True

Python keywords

  • Keywords are reserved words and you cannot use them as constant or variable or any other identifier names.

  • All the python keywords contain lowercase letters only.

anddefexecifnotreturn
assertdelfinallyimportortry
breakelifforinpasswhile
classelseasyncfromprintis
continueexceptgloballambdaraiseyield

Variables

  • Variables are nothing but reserved memory locations to store values. This means when you create a variable, you reserve some space in memory.

  • Based on the datatype of a variable, the interpreter allocates memory & decided what can be stored in the reserved memory.

Assigning values to variables

  • Python variables do not need explicit declaration ton reserve memory space. The declaration happens automatically when you assign a value to variable. The equal sign ( = ) is used to assign value to variables.

Example

main.py

  area_code = 110074 # type - int
  name = "Speed" # type - str (string)
  floating_variable = 12.25 # type - float

  # assigning single values to multiple variables
    a, b, c = 1

  # assigning multiple values to multiple variables
  a, b, c = 12, 13, 14

Deleting a variable

main.py

  a = 5
  print(a) # output: 5
  del a
  print(a) # output: NameError: 'a' is not defined

Best Practices

  1. Consistency Maintain consistent naming conventions throughout your codebase to enhance readability.

  2. Meaningful Names Choose names that provide context and meaning, making your code self-explanatory.

  3. Avoid Ambiguity Steer clear of ambiguous names that may lead to confusion or unintended consequences.

Conclusion

Python Identifiers are the bedrock of coding in Python. By adhering to the naming conventions and best practices outlined in this guide, you'll not only write cleaner and more readable code but also make collaboration with other developers a breeze. Remember, naming is not just a technicality; it's a powerful tool for effective communication in programming.

FAQ

  1. What happens if I use a reserved word as an identifier in Python?

    Using a reserved word as an identifier will result in a syntax error. Choose a different name.

  2. Is there a limit to the length of Python identifiers?

    While Python doesn't impose a strict limit, it's advisable to keep identifiers reasonably short and meaningful.

  3. Can I change the case of an identifier within the same scope?

    No, Python is case-sensitive, so 'myVar' and 'myvar' are considered different identifiers.

  4. Are there any naming conventions for Python classes?

    Yes, class names should be written in CamelCase, starting with an uppercase letter.

  5. What are some common mistakes to avoid when naming identifiers?

    Avoid using single-character names, being inconsistent with conventions, and choosing non-descriptive names.