Tuesday, February 27, 2018

What are the Python Identifiers ?

Python Identifier:

A Python identifier is a name used to identify a variable, function, class, module, or other object. An identifier starts with a letter A to Z or a to z, or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

Python does not allow punctuation characters such as @, $, and % within identifiers.

Python is a case sensitive programming language. Thus,Manpower and manpower are two different identifiers in Python.

Here are naming conventions for Python identifiers:

* Class names start with an uppercase letter. All other identifiers start with a lowercase letter.

* Starting an identifier with a single leading underscore indicates that the identifier is private.

* Starting an identifier with two leading underscores indicates a strongly private identifier.

* If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.


The most important of all Python commands is an assignment statement, such as        temperature = 98.6

This command establishes temperature as an identifier (also known as a name),and then associates it with the object expressed on the right-hand side of the equal sign, in this case a floating-point object with value 98.6.




Figure 1: The identifier temperature references an instance of the float class having value 98.6.

The primary restrictions are that an identifier cannot begin with a numeral (thus 9lives is an illegal name), and that there are 33 specially reserved words that cannot be used as identifiers.


Objects in Python
The semantics of a Python identifier is most similar to a reference variable in Java or a pointer variable in C++. 

Each identifier is implicitly associated with the memory address of the object to which it refers. 

A Python identifier may be assigned to a special object named None, serving a similar purpose to a null reference in Java or C++.

Unlike Java and C++, Python is a dynamically typed language, as there is no advance declaration associating an identifier with a particular data type. 

An identifier can be associated with any type of object, and it can later be reassigned to another object of the same (or different) type. 

Although an identifier has no declared type, the object to which it refers has a definite type. 
In our first example,the characters 98.6 are recognized as a floating-point literal, and thus the identifier temperature is associated with an instance of the float class having that value.

A programmer can establish an alias by assigning a second identifier to an existing object. Continuing with our earlier example, the result of a subsequent assignment, 

                      original = temperature.



Figure 2: Identifiers temperature and original are aliases for the same object.


Once an alias has been established, either name can be used to access the underlying object. If that object supports behaviors that affect its state, changes enacted through one alias will be apparent when using the other alias (because they refer to the same object). 

However, if one of the names is reassigned to a new value using
a subsequent assignment statement, that does not affect the aliased object, rather it breaks the alias. 

Continuing with our concrete example, we consider the command:

                  temperature = temperature + 5.0

The execution of this command begins with the evaluation of the expression on the right-hand side of the = operator. That expression,          temperature + 5.0, is evaluated based on the existing binding of the name temperature, and so the result
has value 103.6, that is, 98.6 + 5.0. 

That result is stored as a new floating-point instance, and only then is the name on the left-hand side of the assignment statement,
temperature, (re)assigned to the result. The subsequent configuration is diagrammed in Figure 3. Of particular note, this last command had no effect on the value of the existing float instance that identifier original continues to reference.




Figure 3: The temperature identifier has been assigned to a new value, while original continues to refer to the previously existing value.