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

Introduction to the function of Python decorator

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "introduction to the function of Python decorator". In daily operation, I believe many people have doubts about the introduction of the function of Python decorator. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "introduction to the function of Python decorator". Next, please follow the editor to study!

Example: looking for treasure. Look for the element 'Gold Coin' in a nested tuple tuple or list list

Import timefrom functools import lru_cachedef find_treasure (box): for item in box: if isinstance (item, (tuple, list)): find_treasure (item) elif item = 'Gold Coin': print (' Find the characteristic') Return Truestart = time.perf_counter () find_treasure (('sth',' sth', 'sth', (' Bad Coin', 'normal coin',' fish', 'sth',' any sth'), ('Bad Coin',' normal coin', 'fish',' sth', 'any sth'),' Gold Coin',)) end = time.perf_counter () run_time_without_cache = end-startprint ('without Cache It took {} s to run. .format (run_time_without_cache) @ lru_cache () def find_treasure_quickly (box): for item in box: if isinstance (item, (tuple, list)): find_treasure (item) elif item = 'Gold Coin': print (' Find the characterization') Return Truestart = time.perf_counter () find_treasure_quickly (('sth',' sth', 'sth', (' Bad Coin', 'normal coin',' fish', 'sth',' any sth'), ('Bad Coin',' normal coin', 'fish',' sth', 'any sth'),' Gold Coin' ) end = time.perf_counter () run_time_with_cache = end-startprint ('in the case of Cache It took {} s to run. .format (run_time_with_cache) print ('Cache is faster than no Cache. .format (float (run_time_without_cache-run_time_with_cache)

Final output

Find the treasure!

Without Cache, it took 0.0002182829999810565 s to run.

Find the treasure!

In the case of Cache, it takes 0.00011638000000857573 s to run.

It is 0.00010190299997248076s faster with Cache than without Cache.

Note: when running this example, my computer is configured as follows

CPU:AMD Ryzen 5 2600RAM:Kingston HyperX 8Gigabytes 2666

Use it for about 7 months.

This decorator can record its input value and run result while the function is running. When the tuple ('Bad Coin',' normal coin', 'fish',' sth', 'any sth') appears for the second time, the function find_the_treasure_quickly with this decorator will not look for the tuple again during recursion, but will directly find the run result in the memo and return it!

At this point, the study on the "introduction to the function of Python decorator" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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