Detailed step by step decorator

Decorator (Decorators) is an important part of Python. Simply put: they are modified functional capabilities of other functions. They help make our code shorter and more Pythonic.

Let's write a simple function, such as seeking a prime number.

def is_prime(num):
    if num < 2:
        return False
    elif num == 2:
        return True
    else:
        for i in range(2,num):
            if num%i == 0:
                return False
        return True

Now let's write a function that outputs prime numbers

def prime_nums():
    for i in range(2,10000):
        if is_prime(i):
            print(i)

Is_prime by calling the function, determined prime number between 2 and 10,000.

If we are to count the time required for this function, then we need to use the time module computing time.

def prime_nums(maxnum):
    start = time.time()
    for i in range(2,maxnum):
        if is_prime(i):
            print(i)
    end = time.time()
    result = end-start
    print(result)

So that we can know the end of this basic function of how long run,
Here Insert Picture Description
if we have a lot of need for such a function to calculate the running time, we can not each have a function like this to write again time.time () it, then python decorators on It played a very convenient role.

We calculate the time to write a decorator

def display_time(func):
    def wapper():
        t1 = time.time()
        func()
        t2 = time.time()
        print(t2 - t1)
    return wapper

func display_time function parameters that we need to use a decorator function, that is the function func as a parameter passed to the display_time this function.
Then call this function passed in wapper this function.


@display_time
def prime_nums():
    for i in range(2,10000):
        if is_prime(i):
            print(i)

Plus @ decorator on function requires the use of a decorator, you can use decorator function.
We run once again
Here Insert Picture Description
you can see basically the same efficiency, our calculation function is also achieved.

If we want to determine the function of the number of prime numbers in prime numbers, and returns a value, decorator how to modify.

@display_time
def prime_nums():
    count = 0
    for i in range(2,10000):
        if is_prime(i):
            print(i)
            count += 1
    return count

Because the function returning, the decorator with a variable needs to receive the page.

def display_time(func):
    def wapper():
        t1 = time.time()
        result = func()
        t2 = time.time()
        print(t2 - t1)
        return result
    return wapper

If the function needs to pass parameters it

@display_time
def prime_nums(maxnum):
    count = 0
    for i in range(2,maxnum):
        if is_prime(i):
            print(i)
            count += 1
    return count

We can use the * args receive function.

def display_time(func):
    def wapper(*args):
        t1 = time.time()
        result = func(*args)
        t2 = time.time()
        print(t2 - t1)
        return result
    return wapper

Here Insert Picture Description
Finally, call the function, consistent with the results obtained with our needs.

Published 73 original articles · won praise 47 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_43870646/article/details/104654612