A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

If tuples are immutable lists, then how is the following possible? lets set an empty tuple n. n = () for i in range(0, 5): n += (i, ) n will return the value (0, 1, 2, 3, 4) we just added element values 0-4 to something thats not supposed to be able to add or remove elements. Is my understanding of immutability just plain wrong here?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think 'var += x' is a shortcut for var = var + x, this creates a new variable using the old one as a base. mutability refers to methods, var.append(x) for example. This is significant in many places, you'll have to learn those on your own. play around with id(y) it translated the value y into a number. you can see when your creating a new list, and when your editing an existing one.

  2. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    As Nessman hinted at, you are assigning something new to n each time, the value of an expression that contains a tuple, but you're not mutating the tuple. You can do >>>string = 'abc' >>>string += 'd' >>>print string abcd even though strings are immutable.

  3. vaboro
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think there is a hint somewhere in lectures 5-7. It was said that the interpreter destroys the link to the old value and creates a link to the new value of the variable; whereas if an object is mutable the link is not destroyed and values can be changed directly without destroying the links... As far as I understood it.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    to get a better ideas of this linking that vaboro mentions. I made a little example >>> List = [1,2,3] >>> Tuple = (1,2,3) >>> def mutate(List, Tuple): List += [4,] Tuple += (4,) print List print Tuple >>> mutate(List,Tuple) [1, 2, 3, 4] (1, 2, 3, 4) >>> List [1, 2, 3, 4] >>> Tuple (1, 2, 3) >>> as you hopefully know, everything done inside a def <name> is in an "isolated" environment. (if you don't know this, it gets explained in a later lecture) this means that any variables created in this environment don't leave the environment. the reason why the list was changed outside the environment was because no new link was created, it edited what was at the end of the original link. this is one of the reasons mutability matters, another is ease of modification. for example >>> List.insert(2,2.5) >>> List [1, 2, 2.5, 3, 4] Try doing that with a tuple and you get an error, fun stuff. Its rather late for me so i hope that was clear, apologies if its not.

  5. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It's pretty simple. You can use an immutable object as a value or as part of an expression, but you can't mutate the object. The number 2 is an immutable object, but we can use it to create new objects by using the 2 as a value a = 2 or as part of an expression a = 2 + 2 The 2 never mutates.

  6. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    A list will mutate, so you can do >>>a = [1] >>>a.append(2) >>>print a [1, 2] >>> You can't do that with a tuple, but you mutate a list using a tuple as the value or part of an expression. >>>a = [1] >>>b = ('a tuple', 'with two strings in') >>>a.append(b) >>>print a [1, ('a tuple', 'with two strings in')] The tuple never mutates.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.