Skip to content

Naty89/Python-Decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Python-Decorator

A Decorator is a function that modifies another function with out modifying the actual function.

example:

from time import time

def performance(func):
  def time_taken(*args, **kwargs):
    t1 = time()
    result = (func(*args, **kwargs))
    t2 = time()
    print(f'This took {t2-t1} ms')
    return result
  return time_taken


@performance
def add(x, y):
  return x+y


print(add(10, 21))

This decorator calculates the amount of time that the add function takes to run, and the result to code would be:

This took 1.1920928955078125e-06 ms
31

Without the decorator, the add function would just simply return 31.

How to write a Decorator

When writing a decorator there is the outer function, in this case it's performance.

def performance(func):

Then there is an inner function which is a wrapping function where something happens before the execution of the function and something happens after the execution of the function. time_taken is the wrapping function or the inner function in this Decorator.

def performance(func):
  def time_taken(*args, **kwargs):
    t1 = time()
    result = (func(*args, **kwargs))
    t2 = time()
    print(f'This took {t2-t1} ms')
    return result
  return time_taken

In this code we get the time before the the function is executed and the time after the function is executed. t1 is time before the function was executed and t2 was the time after we excuted the function, we subtract t1 from t2 to find how long the functon took to run.

2 ways to use a Decorator

The first way:

@performance
def add(x, y):
  return x+y

The second way:

def add(x, y):
  return x+y

t = performance(add)
print(t(10, 21))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages