Describe the difference between objects and values using the terms “equivalent” and “identical”. Illustrate the difference using your own examples with Python lists and the “is” operator.
Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
Finally, create your own example of a function that modifies a list passed in as an argument. Describe what your function does in terms of arguments, parameters, objects, and references.
Create your own unique examples for this assignment. Do not copy them from the textbook or any other source.
1.
· Object is an instance of a class while value is an object that assigns to a variable
· “is” operator is used to test object identity while equivalent operator is used to test the identity of the object based on its values.
· “is” operator is used to know if two objects are equal or not while equivalent (==) is used to know if two operands are equal of not.
Example
l1 = [1,2,3]
l2 = [1,2,3]
if (l1 == l2):
print("True")
else:
print("False")
if (l1 is l2):
print("True")
else:
print("False")
2.
Objects are instances of a class. For example a=[1,2,3], a is the object.
a=[1,2,3]
b=a
print(a is b)
In the above case b is a reference to the object a.
Since the same list above have two different names, a and b are said to be aliased.
Example
x = [1,2,3]
y = [1,2,3]
print(x == y)
print(x is y)
y = x
print(x == y)
print(x is y)
y[0] = 7
print(x)
3.
def manipulateList(list1):
#define a reference made of list1
a = list1
#assign it to a variable b
b = a
#add more values to list a
a.append(20)
#print the lists
print('b', b)
print('a', a)
#reverse the list
a.reverse()
#print the lists
print('b', b)
print('a', a)
#create a new list
a = [12,13,14,15]
print('b', b)
print('a', a)
manipulateList([1,2,3,4,5])
Comments
Leave a comment