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

What is the use of python decorator

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

Share

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

This article mainly introduces the use of python decorator, the article is very detailed, has a certain reference value, interested friends must read it!

1. You can sort the array quickly (with O (N2) time complexity).

Define the insertion sort (arr): for I (1, len (arr)) in the range: J = iMur1 and arr [j] > arr [jacks 1] and j > = 0:arr [j], arr [jacks 1] = arr [jacks 1], arr [j] j-= 1 prints "sorted".

With the help of the decorator, we only need a few lines of code to add timers to every function in the world.

Functions in Python are first-class citizens, that is, they can be passed, assigned to variables, and returned from other functions.

# Anonymous function that returns the current time in milliseconds. Current_millitime = lambda: time () * 100mm this is our even array Sorted array = [x for x in range (100) if not x% 2] # scramble our array to provide it to InsertionSort shuffle (array) # insert sort algorithm definition insert sort (arr): for range I (1, len (arr): J = imur1 and arr [j] > arr [jacks 1] and j > = 0:arr [j], arr [jacks 1] = arr [jacks 1], arr [j] j-= 1 print "sort". Define decorator (function): def wrapper (* args, * * kwargs): T1 = current_millitime () function (* args, * * kwargs) T2 = current_millitime ()-T1 print "" function {} run milliseconds in {} ".format (function.__name__, T2) returns all the contents of the article" what are python decorators for? " Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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