-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDecorators.py
44 lines (35 loc) · 895 Bytes
/
Decorators.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
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.")
func()
return wrapper
@my_decorator
def say_whee():
t = 1
print("Whee!")
print(t)
say_whee1 = my_decorator(say_whee)
#print(say_whee)
#say_whee1()
say_whee()
def do_twice(func):
def wrapper_do_twice(*args, **kwargs):
func(*args, **kwargs)
func(*args, **kwargs)
return wrapper_do_twice
@do_twice
def greet(name):
print(f"Hello {name}")
greet("World")
def do_twice1(func):
def wrapper_do_twice(*args, **kwargs):
func(*args, **kwargs)
return func(*args, **kwargs)
return wrapper_do_twice
@do_twice1
def return_greeting(name):
print("Creating greeting")
return f"Hi {name}"
hi_adam = return_greeting("Adam")