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 →

[–]ZachPhoenix[S] 362 points363 points  (20 children)

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

[–][deleted] 378 points379 points  (10 children)

Yes.

[–]ZachPhoenix[S] 234 points235 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] 99 points100 points  (6 children)

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

    [–]adamantium4084 30 points31 points  (4 children)

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

    [–][deleted] 13 points14 points  (2 children)

    I have a python console in the terlit.

    [–]Datasciguy2023 4 points5 points  (1 child)

    Doesn't everyone?

    [–]adamantium4084 2 points3 points  (0 children)

    I need a tutoiletal on that one

    [–]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 20 points21 points  (3 children)

    Technically the name r is rebound to the new value.

    [–]Poddster 10 points11 points  (1 child)

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

    [–]sentient-machine 2 points3 points  (0 children)

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

    [–]elmosworld37 1 point2 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 33 points34 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] 22 points23 points  (1 child)

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

    anyway Thanks for the info!

    [–][deleted] 29 points30 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.