Identifiers in Python – Naming Rules & Best Practices
Identifiers in Python

Identifiers in Python – Naming Rules & Best Practices

Ways to Define Identifiers in Python 

We can define identifiers in Python in a few ways:

“An identifier is a user-defined name to represent a variable, a function, a class, a module, or any other object.”

“It is a programmable entity in Python- one with a name.”

“It is a name given to the fundamental building blocks in a program.”

You must read about Python Tuples

Python Identifier Naming Rules

a. Rules in Identifiers in Python

So we know what a Python Identifier is. But can we name it anything? Or do certain rules apply? Well, we do have five rules to follow when naming identifiers in Python:

a. A Python identifier can be a combination of lowercase/ uppercase letters, digits, or an underscore. The following characters are valid:

  • Lowercase letters (a to z)
  • Uppercase letters (A to Z)
  • Digits (0 to 9)
  • Underscore (_)

Have a look at Python Number Types

Some valid names are:

  • myVar
  • var_3
  • this_works_too

b. An identifier cannot begin with a digit.

Some valid names:

  • _9lives
  • lives9

An invalid name:

  • 9lives

c. We cannot use special symbols in the identifier name. Some of these are:

!

@

#

$

%

.

d. We cannot use a keyword as an identifier. Keywords are reserved names in Python and using one of those as a name for an identifier will result in a SyntaxError.

e. An identifier can be as long as you want. According to the docs, you can have an identifier of infinite length. However, the PEP-8 standard sets a rule that you should limit all lines to a maximum of 79 characters.

Do you know about Python Variables

b. Lexical Definitions in Python Identifiers

To sum those rules up lexically, we can say:

identifier ::= (letter | “_”) (letter | digit | “_”)*    #It has to begin with a letter or an underscore; letters, digits, or/and underscores may follow

letter ::= lowercase | uppercase #Anything from a-z and from A-Z

lowercase ::= “a” … “z” #Lowercase letters a to z

uppercase ::= “A” … “Z” #Uppercase letters A to Z

digit ::= “0” … “9” #Integers 0 to 9

Best Practices in Identifiers in Python

While it’s mandatory to follow the rules, it is also good to follow some recommended practices:

  • Begin class names with an uppercase letter, begin all other identifiers with a lowercase letter
  • Begin private identifiers with an underscore (_); Note that this doesn’t make a variable private, but discourages the user from attempting to access it
  • Put __ around names of magic methods (use leading and trailing double underscores), avoid doing this to anything else. Also, built-in types already use this notation.
  • Use leading double underscores only when dealing with mangling.

Let’s discuss Python Iterator

  • Prefer using names longer than one character- index=1 is better than i=1
  • Use underscores to combine words in an identifier, like in this_is_an_identifier
  • Since Python is case-sensitive, name and Name are two different identifiers.
  • Use camel case for naming. Let’s just clear the air here by saying camel case is myVarOne and Pascal case is MyVarOne.

Read Complete Article>>

Also Refer-


要查看或添加评论,请登录

Malini Shukla的更多文章

社区洞察

其他会员也浏览了