|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
In order to make use of an object in a Python program, that object must have a name . The name of an object is the label used to identify that object in the text of a program. A given Python object may have zero, one or more names.
Consider the Python statement:
i = 57This statement creates an object named i and binds various attributes with that object. The type of the object is int and its value is 57.
Some attributes of an object, such its type, are bound when the object is created and cannot be changed. This is called static binding. The bindings for other attributes of an object, such as its value, may be changed at run time. This is called dynamic binding.
Consider again the Python statement:
i = int(57)If we follow this statement with an assignment statement such as
j = ithen the names i and j both refer to the same object!
A comparison of the the form
if i == j:
print "equal values"
tests whether the value of the object named i
is the same as the value of the object named j.
(Clearly this is true since i and j name the same object).
However,
it is possible for two distinct objects to have the same value.
In order to test whether two names refer to the same object,
it is necessary to Python is operator like this:
if i is j:
print "same object"