Unformatted text preview:

6.189 - Intro to Python IAP 2008 - Class 6 Lead: Aseem Kishore Lab 7: Introduction to Objects Problem 1 – Exploring values and references So far, we have used variables with types int, float, bool, string, list and tuple. It turns out that some of these are treated as values, whereas others are treated as references. To understand what this means and what it implies for how we program, this problem will help you explore the various types. Start a new Python shell (or restart an existing one), and type in the following: a = “python” b = “python” I told you that in this case, a and b actually refer to the same object in the heap. But how do you really know? Python provides an “id” function that takes a variable and returns the memory address of the object that’s actually referenced. Try this: print id(a) print id(b) print id(a) == id(b) So it turns out the two are in fact the same. We can compare in a more concise way – Python provides an “is” keyword that tells us if two variables reference the same object or not. Try this: print a is b Now try the same code above on something different. Instead of “python”, try the number 5, the number 5.0, and the boolean True. What do you find? In all of the above cases, you can see that all those values (“python”, 5, 5.0, True) do in fact have memory addresses. In fact, every single value in Python has a memory address. For these basic types, however – string, int, float and bool – we will treat them as primitives, meaning that we won’t consider them as objects for the most part (strings are the only exception). We’ll understand why soon. Question 1 – What happens if you say: a = 5 b = 5.0 Are a and b still referencing the same memory address? Do they evaluate to differentnumerical values? To answer, try these two lines: print a is b print a == b Interesting! So as a general law – objects of different types will always reside at different memory addresses. So, a is b will always evaluate to False if a and b are different types. This also tells you – don’t use “is” if == will do. Now, let’s explore lists. Try this: a = [1, 2, 3] b = [1, 2, 3] Are they the same object? Again, you can check with the “is” keyword, or see their actual memory addresses with the “id” function: print id(a) print id(b) print id(a) == id(b) print a is b And again, let’s check if they’re treated as equal: print a == b So lists are treated differently from other things we’ve seen. They’re different objects, but like math, they will be equal if they are made up of the same elements. Let’s really check that they’re different objects: a[1] = 70 print a print b print a == b Changes made to one list don’t affect the other! Question 2 – What happens if you say: a = [1, 2, 3] b = a Are a and b referencing the same object? Are they equal? What happens if you change list a, does list b change as well? This concept is called aliasing, where two variables reference the same object. With the primitive types that we saw above, Python automatically aliased our variables if they were the same value and type. With lists, we have to explicitly alias if we want the same object. Question 3 – Are tuples aliased automatically, like primitive types, or do we have to explicitly alias them like lists?Problem 2 – Exploring mutability From this point on, we need to be consistent with our terms, and we need to be solid on understanding how the computer thinks and works: - To assign a value to a variable means something like: x = 5 y = “hello” z = [1, 2, 3] - To modify or mutate a variable means something like: z.append(4) In general, assignment occurs with the assignment operator (=), and modification/mutation occurs with member functions – we haven’t really learned about these yet, but they occur with the member operator (.) followed by a function that the object supports. In this case, append is a function that all list objects support. However, to make things more confusing, there are statements like these: z[2] = “three” del z[1] The first statement looks like an assignment, and it is – it’s an assignment to a variable inside a list (remember, lists are collections of variables – they’re useful when we have a dynamic number of variables). However, we don’t really care about that aspect. Instead, we care more that the first statement is ALSO a modification – it modifies the list z. Same with the second statement. So for now, treat list index assignment and deletion as modification. From these definitions, we say that an object is mutable if it (the object itself) can be changed in any way. Similarly, we say that an object is immutable if it can’t be, i.e. it doesn’t support any operations or functions that mutate it. So, back to what we were doing. We’ve found that strings are aliased automatically for us, while lists are not. And we’ve found that tuples are also automatically aliased! Remember how we said strings and tuples are similar? This is no different. Both are arguably complex objects (you have to keep track of how many elements/letters you have, and dynamically allocate that much space for them – yes, strings and tuples are implemented on the inside with some sorts of lists!). However, because both are immutable types (as in, we can’t modify any string or tuple), and because both are commonly used repeatedly, Python saves memory by aliasing automatically. But because lists are mutable, Python does not dare alias. Just like we have objects that are mutable and immutable, we can specify it further. Since we know mutability is caused by functions or operations that change an object, we can specify whether functions will cause an object to be mutable or not.We say a function is a pure function if it makes no modifications to the object it’s working on. Similarly, we say a function is a modifier function if it does. Another way of referring to these functions is that they have side effects. Why the term side effects? We’ve been teaching you guys to think of functions as similar to math functions – in general, you shouldn’t take input from within a function, and you shouldn’t print stuff from within a function. Instead, you should return a value, and the function will get evaluated to that value, just like sqrt(4) gets evaluated to 2. Well, this style is so important that in computer science, it’s


View Full Document

MIT 6 189 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?