When we use lists in python we are referring to a

Info icon This preview shows pages 7–10. Sign up to view the full content.

View Full Document Right Arrow Icon
When we use lists in Python, we are referring to a block of data in the computer’s memory. Two different names can refer to the same block. Changing entries in one will change the other one. >> L = [ ’Abdul’ , ’Cris’ , ’Ephraim’ , ’Raoul’ ] >> M = L >> M [2] = ’George’ >> L [’Abdul’,’Cris’,’George’,’Raoul’] 7.28 Aliasing What happened is exactly what happened in Scheme when we mutated fields of structures. Changes to the entries of the list M affected the entries of the list of L . This is because they refer to the same object in memory. This doesn’t happen if I change the entire object that one variable refers to. >> L = [ ’a’ , ’b’ , ’c’ ] >> M = L >> L = [1,2,3] >> M [’a’, ’b’, ’c’] Here, the reassignment to L makes an entirely new list object. But M continues to refer to the previously existing one. 7.29 7
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Aliasing with functions This can also change lists when they are the arguments to functions: We can mutate the function argument, and the changes will change the value in the environment where the function was called. # change_first_to_one: (listof int) None def change_first_to_one ( L ): L [0] = 1 >> A = [44,16] >> change_first_to_one ( A ) >> A [1, 16] When that we call a function, a copy of the argument(s) are given to the parameter(s): in this particular case, a copy of what is inside A is given to L . Specifically, the arrow/pointer to the list is copied from A to L . 7.30 Atomic versus compound objects and mutation This is different than what happens with atomic arguments to functions! # move_to_one: int None def move_to_one ( n ): n = 1 >> my_age = 16 >> move_to_one ( my_age ) >> my_age 16 Here, again, the local variable n does not share space with the variable my_age , since numbers are a simple type, not stored in our table of objects. 7.31 Many many pitfalls! We need to know if a function modifies a list or makes a new one . If it modifies an existing list, then all objects that refer to that list will be changed. If it makes a new one, that new list is a new object, and it will break the connection to existing ones. There’s no aliasing anymore, even if that was what you wanted. Section 10.12 of the book talks about this. It’s worth reading and re-reading. 7.32 List methods, and methods in general Many objects in Python have functions that you can apply to them that are based on their type. We’ve seen some for strings; there are several for lists as well. >> L = [] >> type ( L ) <type ’list’> >> dir ( L ) [..., ’append’, ’count’, ’extend’, ’index’, ’insert’, ’pop’, ’remove’, ’reverse’, ’sort’] Most methods for mutating lists don’t return a value; they just mutate the attached list. 7.33 8
Image of page 8
Methods and their documentation There is a nice string attached to many functions, __doc__ , which tells you what that function does: >> L = [] >> L . append <built-in method append of list object at 0x6b918> >> print L . append . __doc__ L.append(object) -- append object to end This is a very sensible thing to use for functions you’re not entirely sure about.
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern