Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to the function of Python decorator

Shulou Source: shulou.com Published: 2022-06-03 19:15:51 10月04日 Update

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!

Tags: Runs functions situations learning functions more examples results help utility next elements again memos treasures articles methods theories computers knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi OPPO Reno Docker Shulou Information