Python shallow copy feature or defect?

Outline

We first look at the official description of the list / dictionary copy method:

>>> help(dict.copy)
Help on method_descriptor:

copy(...)
    D.copy() -> a shallow copy of D

>>>
>>> help(list.copy)
Help on method_descriptor:

copy(...)
    L.copy() -> list -- a shallow copy of L

You can see the official description of them are shallow copy of the original object (in the article  Python - copy / deepcopy I have explained the concept of copy library shallow / deep copy and shallow copy here concept with copy.copy, not repeat them).

So, we create a dict_instance objects, and then copy_instance to accept the return of dict_instance shallow copy of the results:

>>> dict_instance = {0: [0], 1: [0, 1], 2: [0, 1, 2]}
>>> copy_instance = dict_instance.copy()
>>> dict_instance[0][0] = 1
>>> dict_instance
{0: [1], 1: [0, 1], 2: [0, 1, 2]}
>>> copy_instance
{0: [1], 1: [0, 1], 2: [0, 1, 2]}

Above, we modify the dictionary object list of the first element of the referenced key value "0" is 1, i.e. dict_instance [0] [0] = 1, obviously followed copy_instance such modifications. You can use their own id function see, it is really achieved such a shallow copy copy.

Defect or characteristic?

So far, copy is normal work, also in line with our expectations. From the beginning, I hope shallow copy at all times maintain consistency address, but the python shallow copy (whether copy.copy, list.copy or dict.copy) is able to keep consistency address it?

We know that in Python, tuples, int, string and other types are immutable , in the article referred to our immutable types of container object "Edit" in fact it is the point new object / address , and not place editing .

I hope shallow copy logically keep consistency address, ignored due to "modify" the immutable elements destroyed consistency. but in fact:

>>> from copy import copy
>>> list_instance = [1, 2, 3]
>>> copy_instance = copy(list_instance)
>>> [id(x) for x in list_instance]
[1386622096, 1386622112, 1386622128]
>>> [id(x) for x in copy_instance]
[1386622096, 1386622112, 1386622128]
>>> list_instance[0] = 4
>>> list_instance
[4, 2, 3]
>>> copy_instance
[1, 2, 3]
>>> [id(x) for x in list_instance]
[1386622144, 1386622112, 1386622128]
>>> [id(x) for x in copy_instance]
[1386622096, 1386622112, 1386622128]

Can be clearly seen from the above code to copy.copy method does not address consistency time, when we "Modifying" immutable element destroys this coherence. Similarly we observe list.copy and dict.copy:

>>> list_copy = list_instance.copy()
>>> list_instance
[4, 2, 3]
>>> list_copy
[4, 2, 3]
>>> [id(x) for x in list_instance]
[1386622144, 1386622112, 1386622128]
>>> [id(x) for x in list_copy]
[1386622144, 1386622112, 1386622128]
>>> list_instance[0] = 0
>>> list_instance
[0, 2, 3]
>>> list_copy
[4, 2, 3]

>>> dict_instance = {0: 0, 1: 1, 2: 2}
>>> dict_copy = dict_instance.copy()
>>> [id(x) for x in dict_instance]
[1386622080, 1386622096, 1386622112]
>>> [id(x) for x in dict_copy]
[1386622080, 1386622096, 1386622112]
>>> dict_instance[0] = 3
>>> dict_copy
{0: 0, 1: 1, 2: 2}

Can be clearly observed, lists, and dictionaries built-in copy method performance agreement with the copy.copy, did not remain the same after the "Edit" immutable elements .

This is contrary to what I expected: from my Logically, since it is a shallow copy, and this is Python logic gate close to the natural human language, they should at all times maintain address consistency, the programmer screen all the destruction addresses the consistency of operation , forced to be consistent . But the fact that the results have been set forth above.

I fails to account for whether this is a characteristic of the language, but this is indeed cause an ambiguous understanding of the code, in fact, me and my friends a few days ago when the program will be misinterpreted shallow copy of this kinds of properties, or even because of this characteristic confuse the concept of depth copy, so I think it should be a logical flaw, but if you want to make up for deficiencies, would cause new problems such as compatibility, to consider this situation to be added branch decision, modify the existing operational mechanism, it is a question worth considering.

发布了15 篇原创文章 · 获赞 3 · 访问量 2万+

Guess you like

Origin blog.csdn.net/Christopher_L1n/article/details/86777585