Objects and Variables in Python
In Python, everything is an object, such as int, float, string, list, tuple, dictionary,… When you assign a value to a variable, you basically create an object with a reference to it. When running this code of assignment: a = “Truck”, the computer does 3 separate tasks:
- Create a string object “Truck” (in other words, a string object whose value is “Truck”)
- Put the string object “Truck” somewhere in the computer memory – to check the memory address, use code print(id(“Truck”))
- Establish a reference between variable a and string object “Truck”, so that variable a is pointing to the string object “Truck” created at Task #2. The variable a has the same memory address as indicated by the fact that id(“a”) has the same result as id(“Truck”).
After a = “Truck”, if the code is followed by b = a and c = “Truck”, then the string object “Truck” created at Task #2 above gets two new variables pointing to it: b and c. Together “Truck” now has 3 variables pointing to it: a, b, c.
Code b = “Car” is to created a new string object “Car” in the memory at a different address from “Truck” and variable b is reassigned to point to “Car” while a and c are still pointing to Truck (Figure 3).
A string object in Python is immutable, which means you can not change a string object’s value stored in the memory. For this reason, code d = a.replace(“T”, “S”) does not (or can not) change the string “Truck” stored in the memory, and instead, a new string object “Sruck” is created in the memory and a new variable d is point to it while a is still point to “Truck” (Figure 4). It should be noted that code a = a.replace(“T”, “S”) is valid, however, this code re-assigns variable a to point to the new string “Sruck” (result of a.replace(“T”, “S”)).
A list is another type of object in Python and a variable is pointing to a list object stored in the memory after code like p = [“Tom”, “David”, “Sarah”]. However, since a list object is mutable, you can actually update a list object’s value in memory without creating a new list and all the variables pointing to the list object will have the updated value (Figure 5).
In Figure 5, variable t is pointing to the same list object as p does, so if t gets changed by t.append(“Kate”), p changes too. If this is something not desired, t can be assigned to a copy of p at first by t = p.copy() or t = p[:] and then t and p will be two variables pointing to two different list objects.
if code t = [“Jack”, “Robert”, “Jennifer”] follows the code in Figure 5, which re-assigns variable t to point to a new list object (while variable p is still pointing to the original list object), as a result, variable p and t will point to two different list objects(Figure 6).
When a function is called with the required parameters (or variables) being supplied, what is passed on to the function is the reference relationships between variables and the objects (Figure 7 and Figure 8).
In Figure 8, code var2 = var2 + “Great!” creates a new string object “Hello! Great!” and the variable var2 is re-assigned to point to this new string object in the function. Since the variable String_1 has always been pointing to “Hello!”, after calling string_function, String_1 does not get changed.
A method to pass over the new string object value to String_1 is to use “return” in the function (Figure 9). Code String_1 = string_function(String_1) takes the returned value of var2 which is the new string object “Hello! Great!”, and re-assigns String_1 to point to it. Apparently, if you don’t wish to change String_1, another variable can be assigned to point to the new string object: Another_string = string_function(String_1).
Leave a Reply