Usage of python Identifier and Points for Attention

  • 2021-11-29 07:49:12
  • OfStack

1. Identifiers consist of characters (A ~ Z and a ~ z), underscores, and numbers, but the first character cannot be a number.

2. Reserved characters cannot be the same as identifiers.

3. Cannot contain special characters such as spaces, @,% and $.

4. Letters are strictly case sensitive.

Although identifiers can have underscores, underlined identifiers often have special meanings, so it is not recommended to use underlined identifiers unless you really want to do so.

Instances

Example: The following identifiers are legal:
abcd
abc_d
Abc_3d
IF//python is case sensitive, so if is a reserved word, but IF is not. You can use
The following identifiers are illegal:
3abc//Numbers cannot start
if//Identifier cannot be the same as reserved word
@ abc//Identifiers cannot have special characters such as spaces, @,%, and $

Expansion of knowledge points:

Python identifier naming specification

Simply understood, an identifier is a name, just as each of us has his own name, and its main function is to serve as the name of variables, functions, classes, modules and other objects.

The naming of identifiers in Python is not arbitrary, but should follow the command rules specified in 1, such as: marble platform factory

Identifiers are composed of characters (A ~ Z and a ~ z), underscores, and numbers, but the first character cannot be a number.

Identifiers cannot be the same as reserved words in Python. The reserved words will be described in detail in subsequent chapters.

Identifiers in Python cannot contain special characters such as spaces, @,%, and $.

For example, the identifiers listed below are legal:

UserID name mode12 user_age

The following named identifiers are illegal:

4word # cannot begin with a number try # try is a reserved word and cannot be used as an identifier $money # cannot contain special characters

In Python, the letters in identifiers are strictly case sensitive, that is to say, if two identical words have different sizes and formats, the meanings of multiple representatives are completely different. For example, the following three variables are completely independent and have nothing to do with each other. They are independent individuals.

number = 0 Number = 0 NUMBER = 0

In Python, identifiers beginning with underscores have special meanings, such as:

An identifier beginning with a single underscore (such as _ width), which indicates a class attribute that cannot be directly accessed and cannot be imported by from... import *;

An identifier beginning with a double underscore, such as __add, represents a private member of the class;

Identifiers that begin and end with double underscores, such as __init__, are private identifiers.

Therefore, unless required by specific scenarios, you should avoid using identifiers beginning with underscores.


Related articles: