Save 37% on Get Programming. Just enter code fccbell into the discount code box at checkout at manning.com. Show
Immutable Objects Most python objects (booleans, integers, floats, strings, and tuples) are immutable. This means that after you create the object and assign some value to it, you can’t modify that value. Definition An immutable object is an object whose value cannot change. What does this mean behind the scenes, in computer memory? An object created and given a value is assigned some space in memory. The variable name bound to the object points to that place in memory. Figure 1 shows the memory locations of objects and what happens when you bind the same variable to a new object using the expressions Figure 1. The variable named You can see the value of the memory location to which the object is assigned, using the a = 1 id(a) The value shown represents the location in memory of the object with value 1, accessed by the variable named a = 2 id(a) Like before, the displayed value represents the location in memory of the object with value 2, accessed by the variable named Objects of these types can’t be modified once created. Suppose you have the
following lines of code, which are executed in the order shown. You initialize two variables, a = 1 b = 2 a = 3 Figure 2 shows the objects that exist in your program’s memory with each line of code
The old object with a value of 1 may still exist in computer memory, but you lost the binding to it; you don’t have a variable name as a way to refer to it anymore. Figure 2. Progression of binding variables to objects. On the left, a = 1 shows that the object 1 is at some memory location. In the middle, Once an immutable object loses its variable handle, the Python interpreter may delete the object to reclaim the computer memory it took up, and use it for something else. Unlike some other programming languages, you (as the programmer) don’t have to worry about deleting old objects – Python takes care of this for you through a process called “garbage collection.” Quick Check 1: Draw out a diagram similar to the one in Figure 2 to show variables and objects that they point to (and any leftover objects) for the following sequence of statements. sq = 2*2 ci = 3.14 ci = 22/7 ci = 3 The Need for Mutability Once you lose the variable binding to an object, there’s no way to get back to that object. If you want the program to remember its value, you need to store its value in some temporary variable. Using a temporary variable to store values that you don’t need right now, but may need in the future, isn’t an efficient way of programming. It wastes memory and leads to cluttered code filled with variables that, for the most part, is never used again. If immutable objects are objects whose value can’t change once created, a mutable object is an object whose value can change once created. Mutable objects are often objects that can store a collection of data. Lists (Python type For example, you can make a list of items you’ll need from the grocery store; as you decide what you need, you add them to the list. As you buy things, you remove them from the list. Notice that you’re using the same list and making modifications to it (crossing out or adding to the end) as opposed to having many lists in which you copy over items every time you want to make a change. As another example, you can keep your grocery needs in a dictionary that maps every item you need from the store to a number representing the quantity that you need. Definition A mutable object is an object whose value can change. Figure 3 shows what happens in memory when you bind variables to mutable objects. When you modify the object, you keep the same variable binding and the object at the same memory location’s directly modified. Figure 3. On the left, you have a grocery list at some memory location. On the right, you add another item to your grocery list and the object at the same memory location is directly modified. Mutable objects are more flexible when programming because you can modify the object without losing the binding to it. First, a mutable object can behave the same way as an immutable object. If you rebind a sample grocery list to a variable a = ["milk", "eggs"] id(a) a = ["milk", "eggs", "bread"] id(a) If you have the option to modify the original object directly without losing the binding to it, use operations that only work on mutable objects. In the code below, you append (add to the end of the list) one more item. The memory location of the object that the variable a = ["milk", "eggs"] id(a) a.append("bread") id(a) Mutable objects are useful in programing for several reasons. First, you can store data, which is part of a collection (for example, lists of people or mappings of people to phone numbers), in an object and you can keep the object for use later on. Once the object is created, you can add data to and remove data from the object itself, without creating a new object. Once you have the object, you can also modify elements in the collection by modifying the elements in the object itself instead of creating a new copy of the object with only one of its values modified. Finally, you can rearrange data in the collection by keeping the same object and making the rearrangement in-place – an example is a list of people that you want to sort alphabetically. With huge collections of data, it’d be inefficient to copy your collection into a new object every time you make a change to it. Quick Check 2: Would you use a mutable or an immutable type of object to store the following information?
That’s all for this article. If you want to learn more about the book, check it out on liveBook here and see this Slideshare presentation. What type of variable represents a location in the computer's memory that Cannot change once it has been assigned a value?A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant.
Is a named location in memory that stores a value that does not change for the life of the program?A final is a named memory location which temporarily stores data that remains the same throughout the execution of the program. It is a constant variable in the program. The type of a variable indicates what kind of value it will store. The name of a variable is known as its identifier.
What are variables used to store memory locations?Variables are usually stored in RAM. This is either on the heap (e.g. all global variables will usually go there) or on the stack (all variables declared within a method/function usually go there). Stack and Heap are both RAM, just different locations. Pointers have different rules.
What contains data that Cannot be changed by the user?Read-only memory (ROM) is a type of non-volatile memory used in computers and other electronic devices. Data stored in ROM cannot be electronically modified after the manufacture of the memory device.
|