Immutable vs Mutable Objects in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

ความคิดเห็น • 60

  • @michaelkim733
    @michaelkim733 4 ปีที่แล้ว +18

    wow can i ask what kind of mic you use? the voice is extremely clear

  • @nasert.988
    @nasert.988 3 ปีที่แล้ว +10

    Great video. Just one point about modifying a mutable object inside an immutable object. In fact, Python only checks if the objects inside an immutable object are changing or not. If you take id() from your list object (which is inside your tuple), then you see the memory address of your list object. If you modify your list object, the id() or memory address does not change (because this is the property of a mutable object). Since the new list object is not different from the original one, Python does not care and in its opinion still the immutability of the tuple object is not violated.

    • @nosms6581
      @nosms6581 5 หลายเดือนก่อน

      thanks, this was helpful

  • @kaveeshashah6267
    @kaveeshashah6267 5 ปีที่แล้ว +4

    Hi,
    I have a doubt. We say that the reference to the list doesn't change and hence we can still change the list in a tuple. But why does it stop us in the case of integer? Does the reference change for integers? Sorry I know it sounds stupid bit if you can please clarify it

    • @LauraBeatris
      @LauraBeatris 3 ปีที่แล้ว

      An integer is an immutable value. Every time that it changes, a new reference in the memory is created.

  • @LewisCowles
    @LewisCowles 7 ปีที่แล้ว +5

    Surely the non-transitive part benefits runtime performance as python does not have to traverse objects and structures?
    I actually first noticed Python mutability last year where it stopped me from getting 100% unit tests passing, was a bit of a laughing point when I was done as 5 minutes after I left the interview room I realised what I'd done and made some new rules for the future.
    On the `cost` side of things in terms of processing speed whilst I'm aware CoW (copy on write) semantics exist in some languages how would you feel about a mnemonic or key-word to make copy or copy on write available to python in a more terse way?)

  • @TheSRONIX
    @TheSRONIX 4 ปีที่แล้ว +4

    Thank you! This concept was confusing coming from mainly coding in C.

    • @soumyarastogi6399
      @soumyarastogi6399 4 ปีที่แล้ว

      Check out my TH-cam channel for more on Python

  • @mikijasar2594
    @mikijasar2594 7 หลายเดือนก่อน

    Thanks for your video but I wish you would have done the same for the set because the set does not allow changing mutable elements within the set

  • @HOWYOUDOIN884
    @HOWYOUDOIN884 2 ปีที่แล้ว +1

    Why use the word "mutable"? Reminds me too much of muting something, like a television speaker is mutable (you can make it quiet, or change the volume). Why not just use "changeable" and "unchangeable"?

  • @zulkifal111
    @zulkifal111 6 ปีที่แล้ว +1

    awesome Dan-great clarity-it only gives me a reason to "stalk" { :-)} you on the net-you were crystal clear-im not a programmer by background but dba-ur videos encourages me to take a serious look at python

    • @realpython
      @realpython  6 ปีที่แล้ว

      Thanks for the kind words!

  • @mesartwell
    @mesartwell 4 ปีที่แล้ว +1

    Does anyone know which interpreter is being used? I'm interested in the as-you-type help feature.

  • @_MANOGNAKONDURUNBKRIST
    @_MANOGNAKONDURUNBKRIST ปีที่แล้ว

    we are saying numeric data type is immutable but if we assign a=5,we can change 5 to 6 ....but why it's saying immutable

  • @aalapjethwa6452
    @aalapjethwa6452 6 ปีที่แล้ว +4

    Very well done...amazing work man

    • @realpython
      @realpython  6 ปีที่แล้ว +1

      Thanks for the kind words!

  • @softwaretestingmentor
    @softwaretestingmentor 3 ปีที่แล้ว +1

    Crystal clear explanation! I just wonder who are those 11 people who disliked this video :)

  • @ukrainiansubarer7767
    @ukrainiansubarer7767 3 ปีที่แล้ว +1

    It was partly useful to me. Much more interesting part would be to expand with an explanation of memory management by mutable and immutable objects, this would make clear that "non-transistent state".

  • @fantom_xop
    @fantom_xop 3 ปีที่แล้ว +1

    Today this video helped me a lot

  • @RAHUDAS
    @RAHUDAS 2 ปีที่แล้ว

    U did not mention numbers are immutable too why?? Or i am missing something to understand??

  • @durgapratapbehera8606
    @durgapratapbehera8606 4 ปีที่แล้ว

    Sir can you make a video on how mutability works by using variable assignment

    • @soumyarastogi6399
      @soumyarastogi6399 4 ปีที่แล้ว

      Check out my TH-cam channel for more on Python tutorials

  • @angelsalazar9093
    @angelsalazar9093 5 ปีที่แล้ว +1

    Thanks, Dan . This was useful

  • @SmitPatel-hs2hv
    @SmitPatel-hs2hv 4 ปีที่แล้ว

    8:15 Can you please explain "we can modify the objects thats the tuple " holds

    • @sandy73rocks
      @sandy73rocks 3 ปีที่แล้ว +1

      If a tuple contains a list as one of its elements, we can modify the list itself which is mutable and that change is visible in the tuple.

  • @panjackson4876
    @panjackson4876 4 ปีที่แล้ว +1

    Very helpful! Thanks!

  • @geoptus
    @geoptus 6 ปีที่แล้ว +1

    Hi Dan, thanks for putting on this great tutorial series.
    I tried out and understood the examples here but found the following oddness.
    I did:
    list1 = ['a', 'b', 'c']
    list2 = ['d', 'e', 'f']
    tup1 = ( 42, list1, 'hello')
    tup1
    >(42, ['a', 'b', 'c'], 'hello')
    tup1.index(list1)
    >1
    then,
    tup1[1][1] = 'x'
    tup1
    >(42, ['a', 'x', 'c'], 'hello')
    tup1.index(list1)
    >1
    list1[1] = 'y'
    list1
    >['a', 'y', 'c']
    tup1
    >(42, ['a', 'y', 'c'], 'hello) # tup1 item[1] still points to the (mutated) list1
    tup1.index(list1)
    >1
    also,
    tup1[1] is list1
    >True
    - so far, as I expected
    then,
    list1 = list2 # not sure if this is now a new assignment or a mutation of list1
    list1 is list2
    >True
    list1
    >['d', 'e', 'f']
    tup1
    >(42, ['a', 'y', 'c'], 'hello') # still pointing to the previous contents of list1
    tup1[1] is list1
    >False
    tup1.index(list1)
    >ValueError....etc
    what is tup1[1] now pointing to? it seens no longer pointing at list1
    from where is tup1[1] 'getting' ['a', 'y', 'c'] ? is this list item now simply an indepentent tuple item?
    This surprised me. I was expecting the pointer/link to persist
    python 3.6.4

    • @bissallahekele5320
      @bissallahekele5320 3 ปีที่แล้ว +1

      Recall tuples are immutable, same way tup1[0] = 40 will throw an error, assigning another list to index 1 will be disallowed. But you can access the list and modify the properties in the list.
      tup1[1] still points to the object ['a', 'y', 'c']
      list1 points to list2's object now

  • @komputer7566
    @komputer7566 ปีที่แล้ว

    awesome explanation . thank you !

  • @asthamishra8296
    @asthamishra8296 3 ปีที่แล้ว

    Well explained ..thank you...

  • @benc7910
    @benc7910 2 ปีที่แล้ว

    Itd be more clear if u could explain with diagrams of memory space and cpu

  • @tharmarajashenthan1205
    @tharmarajashenthan1205 2 ปีที่แล้ว +1

    thanks broooo

  • @srappytrex3946
    @srappytrex3946 3 ปีที่แล้ว

    what is this color theme?

  • @NOVAsteamed
    @NOVAsteamed 4 ปีที่แล้ว

    You deserve my sub

  • @saarthakjohari9045
    @saarthakjohari9045 6 ปีที่แล้ว +1

    very well explained

  • @satwikshaw1031
    @satwikshaw1031 2 ปีที่แล้ว

    Thanks man 🙌

  • @sumande7308
    @sumande7308 4 ปีที่แล้ว

    This helped me a lot as i am coming from java..

  • @pradishdadhania
    @pradishdadhania 4 ปีที่แล้ว

    Thanks u sir i clear abt topic

  • @lalosalamanca7131
    @lalosalamanca7131 2 ปีที่แล้ว

    Great video

  • @serotonin14
    @serotonin14 3 ปีที่แล้ว

    Thanks a bunch!!😃

  • @EpmMaxim
    @EpmMaxim 3 ปีที่แล้ว

    Thank you!!

  • @ma.jessacarismabalabala9055
    @ma.jessacarismabalabala9055 2 ปีที่แล้ว

    thank you

  • @anwermohammad2754
    @anwermohammad2754 5 ปีที่แล้ว

    very well explained Thank you

    • @realpython
      @realpython  5 ปีที่แล้ว

      You're very welcome!

  • @ahmedelmawrdy4381
    @ahmedelmawrdy4381 2 ปีที่แล้ว

    thanks alot

  • @narayanpathak221
    @narayanpathak221 3 ปีที่แล้ว

    Awesome 🙌✌️ yeah it's helped

  • @dhedarkhcustard
    @dhedarkhcustard 5 ปีที่แล้ว +1

    It's not a bug with python, it's a feature.

  • @reisaki18
    @reisaki18 3 ปีที่แล้ว

    because list is mutable. is just that simple
    even you put a list in a tuple, its still a list.

  • @muhammadmohsinkhan7354
    @muhammadmohsinkhan7354 6 ปีที่แล้ว

    thanks man. Good video

  • @RayHorn5128088056
    @RayHorn5128088056 ปีที่แล้ว

    Every immutable object can be easily made mutable and changed then made immutable. Python does not support truly immutable objects that cannot be changed.

  • @debjyotichattopadhyay6679
    @debjyotichattopadhyay6679 5 ปีที่แล้ว

    nice work

  • @studio2038
    @studio2038 5 ปีที่แล้ว

    nice video

  • @MrNagios
    @MrNagios 7 ปีที่แล้ว

    thanx!

  • @animekanav3950
    @animekanav3950 ปีที่แล้ว

    thx

  • @KrishnaManohar8021
    @KrishnaManohar8021 4 ปีที่แล้ว

    object vs instance

  • @liangyumin9405
    @liangyumin9405 6 ปีที่แล้ว

    Nice!

  • @amirafrooze7347
    @amirafrooze7347 ปีที่แล้ว

    perfekt