In python programming value objects are identical to variables in that they are used for testing identity of objects based on the value. Is operator is used to test and identity the object identity. Implementation of variables can reuse a single instance of an object.
In Equivalence, value of objects can change within the scope of a mutable list while variables represent the relations between the objects. An object contains both value and identity which is identical to a variable that contains data of the object.
The scope of an immutable object can not be changed. Is operator is used for objects that are identical while === is used for objects that have an equivalence. The operator is returns true if objects pointing to the same object are identical.
The following example returns false since the objects are not equivalent.
a1 = [1, 2, 3, 4]
a2 = [2, 3, 4, 5]
print(a1 == a2)
Example 2 returns true since they are identical.
s1 = "sun"
s2 = "sun"
print(s1 is s2)
An alias relationship is said to exist between references of objects if they refer to the same object allocated in memory during execution. A set is a pair of references that satisfy alias relationships between objects.
a1 = [1, 2, 3, 4]
a2 = [2, 3, 4, 5]
# alias
a2 = a1
a2.append("7")
a1.append("3")
print(a1)
A function that accepts list as argument and returns a modified list.
def modify_list(ls):
a = ls
b = a
print('b', b)
print('a', a)
# reverse
a.reverse()
print('b', b)
print('a', a)
# new list
a = [1, 4, 6, 10, 8, 3]
print('b', b)
print('a', a)
modify_list([1, 2, 3, 4, 5])
Comments
Leave a comment