-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.py
47 lines (33 loc) · 997 Bytes
/
decorator.py
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
37
38
39
40
41
42
43
44
45
46
47
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_whee():
print("Whee!")
say_whee() # All of the above is the same as the next code chunk
"""def my_decorator(func):
def wrapper():
print("Something is happening for the function is called.")
func()
print("Something is happening after the funciton is called.")
return wrapper
def say_whee():
print("Whee!")
say_whee = my_decorator(say_whee)
say_whee()"""
""" # WHY DOES THIS NOT WORK
from datetime import datetime
def not_during_the_night(func):
def wrapper():
if 7 <= datetime.now().hour < 22:
func()
else:
pass # Hush, the neighbors are asleep.
return wrapper
def say_whee():
print("Whee!")
say_whee = not_during_the_night(say_whee)
say_whee()"""