Skip to main content

Naming Rules for Variables

To prevent writing Python code with a "JavaScript flavor," we need to understand the naming rules and conventions for variables in Python.

Naming Conventions

The following are the naming rules that both JavaScript and Python must follow:

  • Variable names must start with a letter or an underscore (_).
  • Variable names can only contain letters, numbers, and underscores (_).
  • Avoid using reserved keywords or built-in function names as variable names.

Naming rules are necessary for code to run, while following appropriate naming conventions is an important measure of code readability. In Python, except for using underscore (_) to separate words in variable and function names and using lowercase letters, other naming conventions are the same as in JavaScript.

Here are the similarities and differences between them:

NameJavaScriptPython
Variable NameuserNameuser_name
Class NameUserPermissionUserPermission
Function NamegetByIdget_by_id
Constant NameDEFAULT_LOGO_URLDEFAULT_LOGO_URL

Forbidden Variable Names

In JavaScript, we often use the variable name str to represent a string.

const str = '123'

In Python, str is a built-in function used to convert a value to a string type. In Python, using str as a variable name will override this built-in function, which can lead to unexpected results in complex systems.

Consider the following Python code:

print(str(123)) # Convert the integer 123 to a string
str = '123' # Override the built-in function str
print(str(123)) # TypeError: 'str' object is not callable

This code will throw an exception during execution because str is now a string type.

There are many other built-in functions in Python. The table below lists some of the commonly used built-in function names in Python that JavaScript engineers often use as variable names. It is important to avoid using them when writing Python code.

Built-in FunctionPurpose
strConvert an object to a string
dictCreate a dictionary object
listCreate a list object
setCreate a set object
tupleCreate a tuple object
intConvert an object to an integer
floatConvert an object to a float
boolConvert an object to a boolean value
lenReturn the length of an object
printPrint an object to the console

You can click here to view all the built-in functions in Python.

tip

If you use the PyCharm IDE to write Python code, when you use a built-in function as a variable name, there will be a warning underline below the variable name.

Resources