-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecorators.py
More file actions
36 lines (29 loc) · 1.25 KB
/
decorators.py
File metadata and controls
36 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
import os
import sys
import functools # You need to import this to use decorators on functions with parameters.
"""
Note: Each decorator takes a function as an input. See the function/argument example file for
more information on this.
The syntax is as follows:
@decorator_name
def function_name():
put_code_here
"""
# ~~~~ The Decorators ~~~~
# This decorator does not work with functions that take parameters or return values.
def simple_decorator(func):
def wrapper():
print("Before the function squad")
func()
print("Finito, fam")
return wrapper
# This decorator can be used on functions with parameters and return values.
def print_function_names(func):
@functools.wraps(func)
def wrapper_decorator(*args, **kwargs): # This allows the function's parameters to be passed along.
print("\nStarted " + str(func.__name__) + "()") # You can do whatever you want before the function.
value = func(*args, **kwargs) # Actually runs the function, and stores the output as a variable.
print("Finished " + str(func.__name__) + "()\n") # You can do whatever you want after the function.
return value # Ensures the return values are passed through.
return wrapper_decorator