Skip to content

Instantly share code, notes, and snippets.

@misterhtmlcss
Created July 23, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterhtmlcss/66e3dff4597bd5c5ebec61cc80091691 to your computer and use it in GitHub Desktop.
Save misterhtmlcss/66e3dff4597bd5c5ebec61cc80091691 to your computer and use it in GitHub Desktop.
"""
✓ 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.
(Very bottom)
✓ 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.
"""
print('\n')
print("1. Primitive behaviours")
# Primitive equivalency test.
j1 = "jeff"
h1 = "jeff"
print('1-jeff', 'bool identical', j1 is h1, 'values', j1, h1)
j2 = "Roger"
h2 = j2
print('2-Roger', 'bool identical', j2 is h2, 'values', j2, h2)
j3 = "jeff"
h3 = j3
j3 = "Suchet"
print('3-Suchet', 'bool identical', j3 is h3, 'values', j3, h3, '\n')
"""
1. Primitive behaviours
1-jeff bool identical True values jeff jeff
2-Roger bool identical True values Roger Roger
3-Suchet bool identical False values Suchet jeff
"""
print("2. Objects behaviours")
# Object equivalency test.
a1 = [1, 2, 3]
b1 = [1, 2, 3]
print('1-arr', 'bool identical', a1 is h1, 'values', a1, b1)
a2 = [1, 2, 3]
b2 = a2
print('2-arr', 'bool identical', a2 is b2, 'values', a2, b2)
a3 = [1, 2, 3]
b3 = a3
a3 = [4, 5, 6]
print('2-arr', 'bool identical', a3 is b3, 'values', a3, b3, '\n')
"""
2. Objects behaviours
1-arr bool identical False values [1, 2, 3] [1, 2, 3]
2-arr bool identical True values [1, 2, 3] [1, 2, 3]
2-arr bool identical False values [4, 5, 6] [1, 2, 3]
"""
"""
Focusing on the suite of examples with #2, we see looking at the output that these two arrays while equivalent in their primitives they are not taking up the same memory space within the computer. This example is like that of two hammers from the same manufacture e.g. Model: SuperHammer; when looking at them we think 'these are the same', but on an absolute definition they are not the same, they are only very similar (Downey, A. 2015). If I bought one, brought it home and then loaned it to a friend and one day when we visited I said "Oh this is where my hammer went" and if it was true, then in this case it would be "identical" (Downey, A. 2015). This is not exactly how the computer version of this works, but it is a relatable example of the difference between equivalent and identical.
Referring to the second set (a2, b2), you can see the boolean is True. This is because the memory space for a2 and b2 is the same location in memory. When that space in the computer memory is altered, then they are both altered; thus logically making them aliases (Downey, A. 2015). The relationship of these two variables with the Object List is called a reference (Downey, A. 2015). Despite it being very different from the physical world, these terms 'alias' and 'reference' are very precise and accurately transcribe their meanings in the physical world into the digital (Downey, A. 2015). If we add a reference (variable) in our essay and you make the same reference (variable) in your essay, then we are referring to the exact same work (object) (Downey, A. 2015). If the referenced author were to go back to their work (object) and make a change, then this change would cascade to the aliases of their work being referenced (Downey, A. 2015).
Reference
"Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press."
"""
print("3. Random example displaying some application of the above")
"""
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.
"""
# Takes in Object list of numbers.
def manipulate_nums(num_list):
print("\nPart 1")
x = num_list # reference made of num_list
y = x # x reference assigned to y
# Same list, but now it's been transformed; Not a NEW LIST!
x.append(1101)
print('y', y)
print('x', x)
print("\nPart 2")
# Same list, but now it's been transformed; Not a NEW LIST!
x.reverse()
print('y', y)
print('x', x)
print("\nPart 3")
# New list is created using new assignment, this means it's not going to affect y, because this gives x a NEW reference rather than altering the original parameter.
x = ["We", "Are", "the", "World"]
print('y', y)
print('x', x)
# Passing in an Object List; which is mutable and can be referenced by variables.
manipulate_nums([10, 20, 30, 40])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment