Immutable vs Mutable Objects in Python

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024

Комментарии • 60

  • @nasert.988
    @nasert.988 3 года назад +9

    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 4 месяца назад

      thanks, this was helpful

  • @michaelkim733
    @michaelkim733 4 года назад +19

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

  • @TheSRONIX
    @TheSRONIX 4 года назад +4

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

  • @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?)

  • @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.

  • @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"?

  • @mikijasar2594
    @mikijasar2594 6 месяцев назад

    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

  • @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!

  • @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".

  • @_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 5 лет назад +4

    Very well done...amazing work man

    • @realpython
      @realpython  5 лет назад +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 :)

  • @mesartwell
    @mesartwell 4 года назад +1

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

  • @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

  • @RAHUDAS
    @RAHUDAS 2 года назад

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

  • @SmitPatel-hs2hv
    @SmitPatel-hs2hv 3 года назад +1

    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.

  • @fantom_xop
    @fantom_xop 3 года назад +1

    Today this video helped me a lot

  • @angelsalazar9093
    @angelsalazar9093 5 лет назад +1

    Thanks, Dan . This was useful

  • @panjackson4876
    @panjackson4876 4 года назад +1

    Very helpful! Thanks!

  • @benc7910
    @benc7910 2 года назад

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

  • @komputer7566
    @komputer7566 Год назад

    awesome explanation . thank you !

  • @durgapratapbehera8606
    @durgapratapbehera8606 4 года назад

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

    • @soumyarastogi6399
      @soumyarastogi6399 4 года назад

      Check out my RUclips channel for more on Python tutorials

  • @asthamishra8296
    @asthamishra8296 3 года назад

    Well explained ..thank you...

  • @tharmarajashenthan1205
    @tharmarajashenthan1205 Год назад +1

    thanks broooo

  • @saarthakjohari9045
    @saarthakjohari9045 6 лет назад +1

    very well explained

  • @satwikshaw1031
    @satwikshaw1031 Год назад

    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

  • @srappytrex3946
    @srappytrex3946 3 года назад

    what is this color theme?

  • @lalosalamanca7131
    @lalosalamanca7131 2 года назад

    Great video

  • @ma.jessacarismabalabala9055
    @ma.jessacarismabalabala9055 2 года назад

    thank you

  • @serotonin14
    @serotonin14 3 года назад

    Thanks a bunch!!😃

  • @NOVAsteamed
    @NOVAsteamed 4 года назад

    You deserve my sub

  • @EpmMaxim
    @EpmMaxim 3 года назад

    Thank you!!

  • @ahmedelmawrdy4381
    @ahmedelmawrdy4381 2 года назад

    thanks alot

  • @anwermohammad2754
    @anwermohammad2754 5 лет назад

    very well explained Thank you

  • @muhammadmohsinkhan7354
    @muhammadmohsinkhan7354 6 лет назад

    thanks man. Good video

  • @MrNagios
    @MrNagios 7 лет назад

    thanx!

  • @animekanav3950
    @animekanav3950 Год назад

    thx

  • @dhedarkhcustard
    @dhedarkhcustard 4 года назад +1

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

  • @narayanpathak221
    @narayanpathak221 3 года назад

    Awesome 🙌✌️ yeah it's helped

  • @studio2038
    @studio2038 4 года назад

    nice video

  • @debjyotichattopadhyay6679
    @debjyotichattopadhyay6679 5 лет назад

    nice work

  • @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.

  • @reisaki18
    @reisaki18 3 года назад

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

  • @KrishnaManohar8021
    @KrishnaManohar8021 4 года назад

    object vs instance

  • @liangyumin9405
    @liangyumin9405 6 лет назад

    Nice!

  • @amirafrooze7347
    @amirafrooze7347 Год назад

    perfekt