Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of python decorator

Shulou Source: shulou.com Published: 2022-06-03 17:38:43 10月19日 Update

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!

Tags: Sort function array purpose content time article scope wrapper help complexity world code value even citizen interest function unit variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Huawei Apple Microsoft