Skip to content

Commit a8d29f0

Browse files
authored
Udemy codealong tutorial
1 parent b99588e commit a8d29f0

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed

Python Decorators..ipynb

+313
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def hello():\n",
10+
" print ('Hello!')\n",
11+
"\n",
12+
"greet = hello"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {},
19+
"outputs": [
20+
{
21+
"data": {
22+
"text/plain": [
23+
"<function __main__.hello()>"
24+
]
25+
},
26+
"execution_count": 2,
27+
"metadata": {},
28+
"output_type": "execute_result"
29+
}
30+
],
31+
"source": [
32+
"greet"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"Hello!\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"greet()"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 4,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"def hello(name):\n",
59+
" print ('Hey this is the hello parent function!!')\n",
60+
" \n",
61+
" def greet():\n",
62+
" return '\\t Hey this is the greet function inside Hello func!!'\n",
63+
" def welcome():\n",
64+
" return '\\t Hey this is the welcome function inside Hello func!!'\n",
65+
" \n",
66+
" if name=='Jit':\n",
67+
" return greet\n",
68+
" else:\n",
69+
" return welcome"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 5,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"Hey this is the hello parent function!!\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"my_func= hello('Jit')"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 6,
92+
"metadata": {},
93+
"outputs": [
94+
{
95+
"data": {
96+
"text/plain": [
97+
"<function __main__.hello.<locals>.greet()>"
98+
]
99+
},
100+
"execution_count": 6,
101+
"metadata": {},
102+
"output_type": "execute_result"
103+
}
104+
],
105+
"source": [
106+
"my_func"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 7,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/plain": [
117+
"'\\t Hey this is the greet function inside Hello func!!'"
118+
]
119+
},
120+
"execution_count": 7,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"my_func()"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": []
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 8,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"#Decorator Creation"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 9,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"def func_needs_decoration():\n",
152+
" print('I need to be decorated!!')"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 10,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"def new_decorator(pass_func_that_needs_decoration):\n",
162+
" \n",
163+
" def wrap_func():\n",
164+
" print('Some code that will come before original!')\n",
165+
" pass_func_that_needs_decoration()\n",
166+
" print('Some code that will come after original!')\n",
167+
" return wrap_func"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 20,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"my_new_func= func_needs_decoration\n",
177+
"decorated_func= new_decorator(my_new_func)"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 21,
183+
"metadata": {},
184+
"outputs": [
185+
{
186+
"name": "stdout",
187+
"output_type": "stream",
188+
"text": [
189+
"I need to be decorated!!\n"
190+
]
191+
}
192+
],
193+
"source": [
194+
"my_new_func()"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 22,
200+
"metadata": {},
201+
"outputs": [],
202+
"source": [
203+
"decorated_func= new_decorator(func_needs_decoration) "
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 23,
209+
"metadata": {},
210+
"outputs": [
211+
{
212+
"name": "stdout",
213+
"output_type": "stream",
214+
"text": [
215+
"Some code that will come before original!\n",
216+
"I need to be decorated!!\n",
217+
"Some code that will come after original!\n"
218+
]
219+
}
220+
],
221+
"source": [
222+
"decorated_func()"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 29,
228+
"metadata": {},
229+
"outputs": [
230+
{
231+
"name": "stdout",
232+
"output_type": "stream",
233+
"text": [
234+
"Some code that will come before original!\n",
235+
"I need to be decorated!!\n",
236+
"Some code that will come after original!\n"
237+
]
238+
}
239+
],
240+
"source": [
241+
"deco= new_decorator(my_new_func)\n",
242+
"deco()"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": null,
248+
"metadata": {},
249+
"outputs": [],
250+
"source": [
251+
"#Syntax on how to add new decorator to existing function."
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": 27,
257+
"metadata": {},
258+
"outputs": [],
259+
"source": [
260+
"@new_decorator\n",
261+
"def func_needs_decoration(): \n",
262+
" print('I need to be decorated!!')"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": 28,
268+
"metadata": {},
269+
"outputs": [
270+
{
271+
"name": "stdout",
272+
"output_type": "stream",
273+
"text": [
274+
"Some code that will come before original!\n",
275+
"I need to be decorated!!\n",
276+
"Some code that will come after original!\n"
277+
]
278+
}
279+
],
280+
"source": [
281+
"func_needs_decoration()"
282+
]
283+
},
284+
{
285+
"cell_type": "code",
286+
"execution_count": null,
287+
"metadata": {},
288+
"outputs": [],
289+
"source": []
290+
}
291+
],
292+
"metadata": {
293+
"kernelspec": {
294+
"display_name": "Python 3",
295+
"language": "python",
296+
"name": "python3"
297+
},
298+
"language_info": {
299+
"codemirror_mode": {
300+
"name": "ipython",
301+
"version": 3
302+
},
303+
"file_extension": ".py",
304+
"mimetype": "text/x-python",
305+
"name": "python",
306+
"nbconvert_exporter": "python",
307+
"pygments_lexer": "ipython3",
308+
"version": "3.8.5"
309+
}
310+
},
311+
"nbformat": 4,
312+
"nbformat_minor": 4
313+
}

0 commit comments

Comments
 (0)