Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial

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

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

  • @adarshsurendran4347
    @adarshsurendran4347 3 года назад +8

    shallow copy = copy @ level 1 only; any changes made at level 2,3,etc in either the original or the copy will reflect in the other.
    deep copy = copy @ all levels; a true independent copy
    ..you covered all possible scenarios of how a shallow copy can be made; also you covered how it will work in case of custom objects. Thanks a lot for clearing this concept for me. Subscribed!

    • @patloeber
      @patloeber  3 года назад +2

      Glad it was helpful and thank you so much!

  • @atwinparamudya1644
    @atwinparamudya1644 2 года назад +2

    Fixes the frustration of so many unnecessary bugs I encoutnered while managing lists thank you!

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

    For many years I understood shallow and deep copy concept very wrongly. Damn!
    Thank you for this video!

  • @jvn8146
    @jvn8146 3 года назад +2

    This video is definitely underrated, I wish I could find the one guy who disliked this ! I got into so many problems in my first python project because I did not know these. Took me 5 hours to find where the problem was and I had to create my own copier functions(which was basically creating new object with same attributes of original objects, in some cases re assigning 15 attribute). I think python tutorials should mention this problem (some of them do) emphasis on its importance and give a practical complete solution for it. I have watched many tutorials and none of them provides a complete and clear and easy solution like you did. Thank you so much

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

    Good stuff dude. Straight and to the point.

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

    A very clear explanation. Thank you

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

    Excellent explanations, thanks

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

      Glad it was helpful!

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

    thanks! I assume then it's always safer to implement deep copy than shallow copy?

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

    Great video. Can you explain what kind of structure is considered 'deep'? From the example in your video, I could tell a list of lists is deep, since it is a nested structure of the same type. But if I have a list of dictionaries, is that considered deep or shallow assuming the values of each dict are plain?

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

    thanks,... nice video + helpful

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

      Glad you like it :)

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

    in the person example when you do a shallow copy with copy.copy(p1) why the age of p1 is not affected? I thought shallow copy copies only the references

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

      Shallow copys are 1 level deep. Modifying on level 1 does not affect the other list. Only if you use the copy asignment operator p1 = p2 then you copy only the references

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

    Great!

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

    Very nice

  • @kvelez
    @kvelez 11 месяцев назад +1

    2:26
    import copy
    class Person:
    def __init__(self, name, age):
    self.name = name
    self.age = age
    class Company:
    def __init__(self, boss, employee):
    self.boss = boss
    self.employee = employee
    p1 = Person("Alex", 55)
    p2 = Person("Joe", 18)
    comp = Company(p1, p2)
    company = copy.deepcopy(comp)
    company.boss.age = 56
    company.employee.name = "Mark"
    print(company.boss.age)
    print(company.employee.name)

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

    good

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

    Hi thank you for your video. It taught me a lot. But I have one question regarding the example at 6:58. I don't understand why the shallow copy can prevent the age of the original from being changed to 28? Thank you!

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

      At 9:36, the change was done at the second level under the level of shallow copy. If changes at the first level under shallow copy won't affect original, as is the case in the person example at 6:58, why would changes two levels below the copy affect the original?

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

      level 1 won't affect a shallow copy, but anything from level 2 and lower does affect it. This is because lower levels are not actually copied, you only copy the reference then.

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

    Great!