How to copy a dictionary and only edit the copy

前端 未结 20 2484
说谎
说谎 2020-11-21 06:59

Can someone please explain this to me? This doesn\'t make any sense to me.

I copy a dictionary into another and edit the second and both are changed. Why is this hap

20条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 07:52

    This confused me too, initially, because I was coming from a C background.

    In C, a variable is a location in memory with a defined type. Assigning to a variable copies the data into the variable's memory location.

    But in Python, variables act more like pointers to objects. So assigning one variable to another doesn't make a copy, it just makes that variable name point to the same object.

提交回复
热议问题