Discussion Assignment Unit6
Discussion Assignment Unit6
Use the terms "equivalent" and "identical" to distinguish between objects and values.
Illustrate the difference further using your own examples with Python lists and the “is” operator.
Using your own Python list examples, explain how objects, references, and aliasing relate to
one another.
Finally, create your own example of a function that modifies a list passed in as an argument.
Hence, 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.
The code and its output must be explained technically whenever asked. The explanation can be
provided before or after the code, or in the form of code comments within the code. For any
descriptive type question, Your answer must be at least 150 words.
End your discussion post with one question related to programming fundamentals learned in
this unit from which your colleagues can formulate a response or generate further discussion.
Remember to post your initial response as early as possible, preferably by Sunday evening, to
allow time for you and your classmates to have a discussion.
When you use information from a learning resource, such as a textbook, be sure to credit your
source and include the URL. Continue to practice using APA format for citations and references.
Solution
In Python, an object is a data structure that has a state and behavior. The state of an object is
represented by its attributes, and the behavior of an object is represented by its methods.
A value is a piece of data that can be assigned to a variable. Values can be numbers, strings, lists,
dictionaries, or other objects.
Two objects can be equivalent if they have the same value while Two objects can be identical if
they are the same object.
For example, the following two lists are equivalent, since they have the same element though not
identical, since they aren’t the same object. This can be proved by using the “is” statement, from
the below the output “false” will be printed. Since the two lists are not identical
list1 = [1,2,3]
list2 = [1,2,3]
print(list1 is list2)
Output
The following two lists are identical, since they have the same element and the same object. This
can be proved by using the “is” statement, from the below the output “true” will be printed.
Since the two lists are identical
list1 = [1,2,3]
list2 = list1
print(list1 is list2)
Output
When a variable is assigned to an object, a reference to the object is created. The reference is a
pointer to the object in memory. Two variables can refer to the same object. This is called
aliasing.
For example, the following code creates two variables that refer to the same list:
list1 = [1,2,3]
list2 = list1
The two variables, list1 and list2, are aliases of each other. They refer to the same object in
memory.
When you modify a list through one of its aliases, the change is reflected in all of the aliases.
For example, the following code modifies the list through the alias list2:
list1 = [1,2,3]
list2 = list1
list2.append(4)
The list is now [1, 2, 3, 4].The change is reflected in both list1 and list2.
list1 = [1,2,3]
list2 = list1
list2.append(4)
list= [1, 2, 3]
modify_list(list)#Calls the modify function
print(list)
Output
The function modify_list modifies the list that is passed to it. The change is reflected in the
original list.
In conclusion, objects, references, and aliasing are important concepts in Python. It is important
to understand these concepts in order to write efficient and correct code.
References
Py4e-int - https://runestone.academy/ns/books/published/py4e-int/lists/objectsValues.html
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf