Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses copy and deepcopy

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how python uses copy and deepcopy. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Rational use of copy and deepcopy

For objects with data structures such as dict and list, direct assignment is done by reference. In some cases where you need to copy the entire object, you can use copy and deepcopy in the copy package. The difference between these two functions is that the latter is copied recursively. The efficiency is also different: (the following programs run in ipython)

The-n after timeit indicates the number of runs, and the last two lines correspond to the output of two timeit, the same below. It can be seen that the latter is an order of magnitude slower.

Import copya = range (100000)% timeit-n 10 copy.copy (a) # run copy.copy (a)% timeit-n 10 copy.deepcopy (a) 10 loops, best of 3: 1.55 ms per loop10 loops, best of 3: 151ms per loop on "how python uses copy and deepcopy" this article ends here. I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report