CS1011 Unit 6 Sample Solution

 Sample Solution


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.  


Objects are something that variables can refer to, and within this object, the object has a type and a value (Downey, 2015).  This is not very intuitive, but using the code below, I will attempt to explain the difference.  


numObj_1 = 20

numObj_2 = 20

listObj_1 = [20]

listObj_2 = [20]


print('line1: value numObj_1 == numObj_2:',numObj_1 == numObj_2) #returns true

print('line2: object numObj_1 is numObj_2:',numObj_1 is numObj_2) #returns true

print()


print('line3: value listObj_1 == listObj_2:',listObj_1 == listObj_2) #returns true

print('line4: object listObj_1 is listObj_2:',listObj_1 is listObj_2) #returns false

First, the output of the code above:


line1: value numObj_1 == numObj_2: True

line2: object numObj_1 is numObj_2: True


line3: value listObj_1 == listObj_2: True

line4: object listObj_1 is listObj_2: False

On the code above the numObj_1 and numObj_2 are identical - they point to the same object, and thus they naturally have the same value of 20.  We tested this on line 1 of the output where the values of the two were identical by using the == operator, and also the two objects are the same , and we tested this using the is operator on the line 2 of the output. 


However, line 3 show that the listObj_1 and listObj_2 are equivalent -- that their value of single element 20 is the same, line 4 shows that they are not identical, as the two object return false when try the is operator between these two objects. 




Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.


We can just modify the above script and add couple lines of code to explain the differences between objects, references, and aliasing.  First, the code and the output below:  


listObj_1[0] = listObj_1[0]+1

print('line5: listObj_1:', listObj_1) #returns 21

print('line6: listObj_2:', listObj_2) #returns 20

print()


#now make the two objects identical

listObj_1 = listObj_2

listObj_1[0] = listObj_1[0]+1

print('line7: listObj_1:', listObj_1) #returns 21

print('line8: listObj_2:', listObj_2) #returns 21

The output from above:


line5: listObj_1: [21]

line6: listObj_2: [20]


line7: listObj_1: [21]

line8: listObj_2: [21]

We saw from first example that listObj_1 and listObj_2 were not identical objects, because when we tried the is operator to check this, it returned false.  That is, there are two different associations of a variable to these objects, even though the values might were initially the same.  This is association is called a reference (Downey, 2015).  To test this, I added 1 to the listObj_1's only element (of 20), and printed out both listObj_1 and listObj_2 on output lines 5 and 6.  As we can see, line 5 shows that listObj_1 has a value of 21, as it was incremented by 1, but listObj_2 remains unchanged  on output line 6, because the reference to the variable was not to the same object.


However,  once we use the assignment operator and code listObj_1 = listObj_2, and are now referencing the same object, and there is now only two references to the a single object.  That is to say, if I change one object, it will now affect the other.  Once I make the two objects the same, and I now do the same, add 1 to listObj_1's element (but not listObj_2).  Then I print out the results on output lines 7 and 8 above and it shows that the changes show on both listObj_1 and listObj_2 variables, as they now both point to only one object.  And this is an example of aliasing, where one object has more than one reference (or variable association).




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. 


Below is a very simple function which takes in a list filled with numeric objects, and squares them.  


testList = [1,2,3,4,5,6,7,8,9,10]


#function square list

def sqList(listArg):

    for i in range(len(listArg)):

        listArg[i] = listArg[i]**2

And the output for above:


Bef modification - testList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Aft modification - testList: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

What we see here is that when we pass in the testList with elements filled with integers from 1 to 10, the function gets a reference to the list (Downey, 2015).  That is, if we modify the list argument (listArg in above case), the original testList will also change both testList and listArg are referencing the same object.  


To show this, the sqList function above simply squares all the elements, but doesn't return it.  However, when we print out the output of  testList before and after calling the sqList function, we see that the elements in the object have changed by the square abount (e.g. 2 -> 4, 3 -> 9, ...and so on) 




References


Downey, Allen. (2015). Think Python: How to Think Like a Computer Scientist. Needham, MA: Green Tea Press

Comments

Popular posts from this blog

CS1011 Unit 5 Sample Solution

CS1011 Unit 3 Sample Solution

CS1011 Unit 4 Sample Solution