Mutable vs Immutable - Python

Поділитися
Вставка
  • Опубліковано 29 лип 2024
  • In this video I go over a very detailed explanation of mutable and immutable objects in python. This is an extremely important concept and is is vital that you understand it if you program with python. Mutable objects are changeable and act differently in memory than other objects. You can create copies and aliases of them. Immutable objects are standard data types that cannot be changed or aliased.
    Mutable Objects:
    - dict
    - list
    - set
    Immutable Objects:
    - int
    - str
    - float
    - bool
    etc..
    Want To Support This Channel?
    Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
    Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
    Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
    Please leave a LIKE and SUBSCRIBE for more content!
    Tags:
    - Tech
    - Tech With Tim
    - Python Tutorials
    - Mutable Objects
    - Immutable objects
    - Mutable vs Imuttable python
    - Python mutable
    - Python immutable
  • Наука та технологія

КОМЕНТАРІ • 45

  • @kriz1718
    @kriz1718 5 років тому +47

    One correction: at 7:58 you are saying that when we create a new string x="str" and assign y=x , y points to a new location but in reality y points to the same location as x. Only if we change y then will y point to a different location. See the code below.
    x='str'
    y=x
    print(x is y)
    y='newStr'
    print("x is", x, "and", "y is ", y)
    output:​
    True
    x is str and y is newStr
    Please correct me if i am wrong

    • @user-if9my3ee3w
      @user-if9my3ee3w 5 років тому +7

      You're right. It can also be checked by this way:
      >>> x = 'str'
      >>> y = x
      >>> id(y) == id(x)
      True

    • @nlke182
      @nlke182 4 роки тому +2

      Yeah, this part was totally wrong in the video, I had to double check it myself since it didn't make sense in the video. Glad someone else caught this and posted the correctiong.

    • @williamwild8812
      @williamwild8812 10 місяців тому

      This guys gets a lot wrong. So misleading to new programmers

  • @terra_creeper
    @terra_creeper 2 роки тому +27

    one important thing you didn't explain that well is that python treats variables differently than other languages
    for example in java, variables are containers for data whereas in python, variables are names pointing to objects in memory
    so when you assign a value to a variable, you actually just change which place in memory that variable is pointing to
    under this light, the difference between immutable and mutable types becomes clearer:
    mutable types are objects you can directly modify without changing the id of the object or creating a new one, so all variables pointing to it will continue pointing to that modified object
    immutable types are objects you cannot modify, you can only create new objects based on the object you wanted to change, so the old object and all variables pointing to it will remain unchanged
    so if you enter:
    x = "str"
    y = x
    you will have two variables pointing to the same string object in memory
    but if you enter:
    y = x + "s"
    you create a new object in memory and let y point to it
    but if you enter:
    x = [1,2,3]
    y = x
    y.append()
    you will still have two variables pointing to the same object, because the append() method directly modifies the list without creating a copy

    • @Hamheadon
      @Hamheadon 2 роки тому

      Is there a deeper reason as to why the CPU creates copies for some data types and doesn't for the rest? Or is that just how it is?

    • @terra_creeper
      @terra_creeper 2 роки тому

      @@Hamheadon it doesnt have anything to do with the cpu, its just how python implemented these data types

    • @GULLAPUDILIKITHBCE
      @GULLAPUDILIKITHBCE 2 роки тому

      thanks best explanation

    • @GULLAPUDILIKITHBCE
      @GULLAPUDILIKITHBCE 2 роки тому

      what i understand is:
      if i copy a mutable object since it can change without changing ids therefore it will effect parent object also
      if i copy immutable object since it will change ids when it is changed therefore it wont effect parent object......
      thanks even after this lecture i can't find out why this happens but after reading comment i got some clarity

  • @beckybonny7552
    @beckybonny7552 Рік тому

    Man you have a gift. Nobody explains this stuff as clearly and logically as you do.

  • @chinz3614
    @chinz3614 2 роки тому +5

    9:45 x and y will be pointing to the same string (at same location).

    • @ahmedramzy892
      @ahmedramzy892 6 місяців тому

      yes it have a same address of same obj

  • @yuan6780
    @yuan6780 4 роки тому +2

    Thanks for this super important concept. Love it.

  • @elkady-prodesiner4172
    @elkady-prodesiner4172 3 роки тому +2

    best teacher ever i watched most of your tutorials and i will watch it all, till i be like you thanks for being here for us ♥

  • @eliasnoecollmartin3681
    @eliasnoecollmartin3681 5 років тому +4

    totally unknown to me. Thanks and greetings from Spain

  • @anonymouswanted3686
    @anonymouswanted3686 2 роки тому +1

    Clearly one of the best python youtubers ever

  • @karimalaa204
    @karimalaa204 3 роки тому

    so good bro I finished learning python and didn't even know about this concept at all LOL tysm

  • @vishwasmagotra5266
    @vishwasmagotra5266 4 роки тому +1

    Very well explained brother, keep up the good work

  • @niloydey6147
    @niloydey6147 2 роки тому

    Very helpful. Thanks for uploading.

  • @tunamusic2314
    @tunamusic2314 2 роки тому +2

    i got one thing i don't understand,
    x = 'str'
    y = x
    you said it create two different objects, but why when i print(id(x)) and print(id(y)) they print the same, it means they have the same id ( address) and points the same value ( 'str' here)

    • @terra_creeper
      @terra_creeper 2 роки тому +2

      When you assign a value to a variable you always change which object the variable points to, even with mutable types.
      so when you type:
      x = "str"
      you create a new string object and let 'x' point to it
      and by typing
      y = x
      you let y point to the same object
      The actual difference between mutable and immutable types is that
      with mutable types, you can modify the actual object,
      but with immutable types you can only ever create new objects based on the first

    • @s.muneeba3944
      @s.muneeba3944 2 роки тому +1

      yeah theres a bit of a mistake in the video above. you can think of immutable objects creating a "temporary" alias meaning they have the same id as long as u dont change it but as soon as u change either x or y then they both become completely different objects with different locations. and fun fact: if you decide to change them back like for eg you had x=5 and y=5 (same ids) and then you changed y x=5 and y=7 (diff ids) NOW if you changed x to be equal to 7 then both would have the same ids again

  • @gopalsv5230
    @gopalsv5230 3 роки тому

    cool and simple ... thnks

  • @rinqiu5300
    @rinqiu5300 3 роки тому

    Very helpful. Any other video of yours that I should watch for more tips/important concept? I'm new to Python. Thanks! :)

  • @joeljose182
    @joeljose182 3 роки тому

    Wow you saved my life thanks

  • @kasyapdharanikota8570
    @kasyapdharanikota8570 3 роки тому

    very well explained

  • @mjj2u2
    @mjj2u2 2 роки тому

    Well done.

  • @krypton9378
    @krypton9378 3 роки тому

    Respect from Bangladesh!

  • @blmppes9876
    @blmppes9876 5 років тому +2

    Love you!

  • @gibbogle
    @gibbogle 2 роки тому

    It looks as if equating a mutable y to x, for example: x = y, is like using a pointer in Fortran: x => y, where x is a pointer.

  • @user-rq9kd7xu5x
    @user-rq9kd7xu5x 3 роки тому

    You are amazing 🤩

  • @mikijasar2594
    @mikijasar2594 5 місяців тому

    Thanks for your video. I am a beginner in learning programming, but I have a small problem with understanding this video of yours. In your video at 8.15 min, you say that x is one string 'str' and y points to another 'str'. So according to you, they are two different objects. Then how do you explain that their id() is the same?? so it's the same object ?? Please reply and thanks in advance
    x = 'str'
    y = x
    print(id(x), x, 'x')
    print(id(y), y, 'y')
    x += '5'
    print(id(x), x, 'x')
    print(id(y), y, 'y')
    ------------------------------------
    2847580343728 str x
    2847580343728 str y
    2847588873840 str5 x
    2847580343728 str y

  • @Zaphodikus
    @Zaphodikus 5 років тому +2

    A slightly longer way of explaining this, is brought to you in a beautiful blog post I found while googling a good answer, which is sadly nowhere to be found. The second best I got was nedbatchelder.com/text/names.html I found, but your clip explains it so much more accessibly.

  • @victorlefeu
    @victorlefeu 3 роки тому

    thanks!

  • @kartiksharma6576
    @kartiksharma6576 3 роки тому

    Grt

  • @rollsamantha2673
    @rollsamantha2673 3 роки тому

    my man

  • @lebdesmath2510
    @lebdesmath2510 2 роки тому

    you should get a "huioin" graphic tablet to upgrade ur writing!

  • @sherlock_221
    @sherlock_221 2 роки тому

    0:00 intro
    3:30 alias and cloning
    6:40 memory model
    Start from 9:25

  • @biraj4893
    @biraj4893 2 роки тому +2

    16:42 ==> x=1000 and y=1000 have same id value. not different.

  • @fumano2679
    @fumano2679 Рік тому

    Fortnite on Desktop thats my man!

  • @goldiak4177
    @goldiak4177 Рік тому

    You didnt active your windows , 😅 just kiding

  • @tomjones8293
    @tomjones8293 Рік тому

    pass by value = immutable and pass by reference = mutable... done. simple

  • @williamwild8812
    @williamwild8812 10 місяців тому

    At 9:10 you are wrong especially by the definition of immutable you CANT change 'str' to 'str5'.
    In idle:
    x = str
    x
    'str'
    id(x)
    4349951664
    x+=5
    x
    'str5'
    id(x)
    4384082480
    The ids show it is a new memory location. You cant change 'str' . Its freaking immutable.
    Watch this video with a grain of salt and test things yourself.