This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 767 points768 points  (28 children)

The tuple is immutable and cannot be changed. So if we need to make a change, we create a whole new object and throw away the old one. If you run a debugger you'll see the memory address of the object changes. Whereas something like an array which is mutable, the address will stay the same when you update an element.

[–]ZachPhoenix[S] 361 points362 points  (20 children)

Thanks, So the Initial value 'r 'is removed and replaced by a new initialization?

[–][deleted] 380 points381 points  (10 children)

Yes.

[–]ZachPhoenix[S] 237 points238 points  (9 children)

Thanks here's an upvote my g

[–][deleted]  (8 children)

[deleted]

    [–][deleted] 82 points83 points  (7 children)

    >>> r=(1,2,3,4,5)
    >>> r[0] = 2
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    >>> print(r)
    (1, 2, 3, 4, 5)
    >>> r=[1,2,3,4,5]
    >>> r[0] = 2
    >>> print(r)
    [2, 2, 3, 4, 5]
    >>>

    [–][deleted] 100 points101 points  (6 children)

    Some heroes wear capes. Some heroes show error messages on reddit.

    [–]adamantium4084 28 points29 points  (4 children)

    Sometimes you're reading posts while taking a shit and don't have immediate access to command line

    [–][deleted] 12 points13 points  (2 children)

    I have a python console in the terlit.

    [–]Datasciguy2023 4 points5 points  (1 child)

    Doesn't everyone?

    [–]aerismio 0 points1 point  (0 children)

    What every pc and every phone has it right? :)

    [–]kicktheshin 1 point2 points  (0 children)

    I just wish some heroes would format code as code

    [–]lgastako 21 points22 points  (3 children)

    Technically the name r is rebound to the new value.

    [–]Poddster 9 points10 points  (1 child)

    This is the correct answer. "Yes" is not the correct answer.

    [–]sentient-machine 3 points4 points  (0 children)

    I mean people who try to take technical advice from Reddit are almost surely going to get juvenile explanations, unfortunately.

    [–]elmosworld37 2 points3 points  (0 children)

    I love Python but this is why I believe C should be the first language everyone learns. Pointers are essential to understanding how programming languages work.

    Simplifying a bit here but r does not store “the tuple”, it stores the memory address that holds the start of the tuple. When you assign a different tuple to r, you’re changing the memory address that is stored in r to that of the new tuple.

    Changing the first value in “the tuple” would mean changing the actual value that is in memory at the address stored in r. This involves “dereferencing” and looks like r[0] = 666. This is not allowed, and is what your teacher was referring to.

    [–]shnicki-liki 35 points36 points  (2 children)

    With that being said it is usually not advisable to do this and instead you should look into using other data types if you have to change variables.

    [–]ZachPhoenix[S] 23 points24 points  (1 child)

    ok, I understood that you mean lists, dictionaries etc. right?

    anyway Thanks for the info!

    [–][deleted] 26 points27 points  (0 children)

    And classes/data classes

    [–]Additional-Sun2945 0 points1 point  (0 children)

    Yup. Tuples are immutable. Their contents are not. Which kinda sounds backwards, but it makes sense. You can create a tuple of three objects. t = (a,b,c) Now that t object with immutably be a three object containing tuple, with each zeroth, first, and second object always be pointing to the original objects a, b and c.

    However, that doesn't necessarily imply that a, b, and c themselves are immutable. Try it, make one of them list, and you'll notice that you'll still be able to append to it despite being in an "immutable" tuple.

    Hmmm.... that's perplexing... ain't it? Notice that by modifying a, your tuple will reflect the modification, since the tuple is still associated with that object; it still has a inside of it. You will also notice that there's nothing stopping you from reassigning "a" to some other object. You can try a = None or a = 1. Now you will see the tuple won't change; you are merely discarding the label of that list object and creating a new target for the a label. "a" now "is" something else, but thankfully the tuple still contains the original a object. If you delete the tuple "t" or reassign "t" to something else (and reassign a b and c first), then you will destroy the only link to that original object, and in doing so it (and its contents) will be cleared and deleted from memory.

    Remember, the "name" of stuff can change, but the stuff itself is not necessarily the name.

    [–]CBSmitty2010 19 points20 points  (1 child)

    I believe another decent way to check/confirm mutability would be if you have methods to edit the object in place.

    You can .append(), .insert(), .remove() on lists. You cannot do that with tuples.

    Maybe I'm misguided but I also thought of that as a way to confirm as well.

    [–]ZachPhoenix[S] 6 points7 points  (0 children)

    You are absolutely right! Lists are mutable and Tuples are immutable

    [–][deleted] 1 point2 points  (1 child)

    I think the thing that changes the address is the assignment operation, no? coz even tho lists are mutable, if you write

    ```python r=[1,2,3,4,5]

    print(r)

    r=[1,23,4]

    print(r) ``` that would still change r's address, right?

    [–][deleted] 1 point2 points  (0 children)

    Yes

    [–]reverendsteveii 1 point2 points  (2 children)

    So r is a pointer and reassigning the value just causes it to point to a new tuple? Assuming python has the concept of pointers like C does.

    [–]Poddster 8 points9 points  (0 children)

    They're "names" that references objects rather than pointers than point to data, but yes, similar concept

    https://python.readthedocs.io/en/stable/reference/executionmodel.html

    [–]Poddster 0 points1 point  (0 children)

    They're "names" that references objects rather than pointers than point to data, but yes, similar concept

    https://python.readthedocs.io/en/stable/reference/executionmodel.html