Skip to content

Commit c60871f

Browse files
more examples about decorators
1 parent ae1f160 commit c60871f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

decorators.py

+57
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,60 @@ def hello(name="Steve"):
1919

2020

2121
print(hello())
22+
23+
24+
def hello_2(name="Steve"):
25+
print("The hello_2() function has been run!")
26+
27+
def greet():
28+
return "This string is inside greet()"
29+
30+
def welcome():
31+
return "This string is inside welcome()"
32+
33+
if name == "Steve":
34+
return greet()
35+
else:
36+
return welcome()
37+
38+
39+
x = hello_2()
40+
41+
print(x)
42+
43+
44+
def hello_3():
45+
return "Hi Steve"
46+
47+
48+
def other(func):
49+
"""
50+
we could pass in func to denote this function takes in another function as a parameter!!!
51+
:param func:
52+
:return:
53+
"""
54+
print("Hello")
55+
print(hello_3())
56+
57+
58+
other(hello_3())
59+
60+
61+
def new_decorator(func):
62+
63+
def wrap_func():
64+
print("Code here before executing func()")
65+
func()
66+
print("Func() has been called")
67+
68+
return wrap_func
69+
70+
71+
@new_decorator
72+
def func_needs_decorator():
73+
print("This function is in need of a decorator!")
74+
75+
76+
# This below line could be replaced by using @new_decorator on the function
77+
# func_needs_decorator = new_decorator(func_needs_decorator)
78+
func_needs_decorator()

0 commit comments

Comments
 (0)