-
Notifications
You must be signed in to change notification settings - Fork 3
/
decorators.py
78 lines (48 loc) · 1.33 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
s = 'GLOBAL VARIABLES'
def simple_func():
"""
locals() and globals() are two convenient Python built in function calls.
"""
my_local_var = 10
print(locals())
print(globals()['s'])
print("All global variables are: ", globals())
simple_func()
def hello(name="Steve"):
return "Hello " + name
print(hello())
def hello_2(name="Steve"):
print("The hello_2() function has been run!")
def greet():
return "This string is inside greet()"
def welcome():
return "This string is inside welcome()"
if name == "Steve":
return greet()
else:
return welcome()
x = hello_2()
print(x)
def hello_3():
return "Hi Steve"
def other(func):
"""
we could pass in func to denote this function takes in another function as a parameter!!!
:param func:
:return:
"""
print("Hello")
print(hello_3())
other(hello_3())
def new_decorator(func):
def wrap_func():
print("Code here before executing func()")
func()
print("Func() has been called")
return wrap_func
@new_decorator
def func_needs_decorator():
print("This function is in need of a decorator!")
# This below line could be replaced by using @new_decorator on the function
# func_needs_decorator = new_decorator(func_needs_decorator)
func_needs_decorator()