Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 000f374

Browse files
Add files via upload
1 parent e48510f commit 000f374

File tree

7 files changed

+1229
-0
lines changed

7 files changed

+1229
-0
lines changed

21-Decorators.ipynb

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def func(new_func):\n",
10+
" print(\"func started\")\n",
11+
" new_func()\n",
12+
" print(\"func ended\")"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"def hello_func():\n",
22+
" print(\"hello world\")"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"name": "stdout",
32+
"output_type": "stream",
33+
"text": [
34+
"func started\n",
35+
"hello world\n",
36+
"func ended\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"func(hello_func)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 14,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"def new_func():\n",
51+
" print(\"new func\")\n",
52+
" def new_func_2():\n",
53+
" print(\"new func 2\")\n",
54+
" return new_func_2"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 15,
60+
"metadata": {},
61+
"outputs": [
62+
{
63+
"name": "stdout",
64+
"output_type": "stream",
65+
"text": [
66+
"new func\n"
67+
]
68+
},
69+
{
70+
"data": {
71+
"text/plain": [
72+
"<function __main__.new_func.<locals>.new_func_2>"
73+
]
74+
},
75+
"execution_count": 15,
76+
"metadata": {},
77+
"output_type": "execute_result"
78+
}
79+
],
80+
"source": [
81+
"new_func()"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 16,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"name": "stdout",
91+
"output_type": "stream",
92+
"text": [
93+
"new func\n"
94+
]
95+
}
96+
],
97+
"source": [
98+
"new_string = new_func()"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 17,
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"new func 2\n"
111+
]
112+
}
113+
],
114+
"source": [
115+
"new_string()"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 19,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"def decorator_function(func):\n",
125+
" \n",
126+
" def wrapper_function():\n",
127+
" \n",
128+
" print(\"wrapper started\")\n",
129+
" \n",
130+
" func()\n",
131+
" \n",
132+
" print(\"wrapper stopped\")\n",
133+
" \n",
134+
" return wrapper_function"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 21,
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"def func_new():\n",
144+
" print(\"hello world\")"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 23,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"example_function = decorator_function(func_new)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 25,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"name": "stdout",
163+
"output_type": "stream",
164+
"text": [
165+
"wrapper started\n",
166+
"hello world\n",
167+
"wrapper stopped\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"example_function()"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 30,
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"@decorator_function\n",
182+
"def func_new():\n",
183+
" print(\"hello world\")"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": 31,
189+
"metadata": {},
190+
"outputs": [
191+
{
192+
"name": "stdout",
193+
"output_type": "stream",
194+
"text": [
195+
"wrapper started\n",
196+
"hello world\n",
197+
"wrapper stopped\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"func_new()"
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": null,
208+
"metadata": {},
209+
"outputs": [],
210+
"source": []
211+
}
212+
],
213+
"metadata": {
214+
"kernelspec": {
215+
"display_name": "Python 3",
216+
"language": "python",
217+
"name": "python3"
218+
},
219+
"language_info": {
220+
"codemirror_mode": {
221+
"name": "ipython",
222+
"version": 3
223+
},
224+
"file_extension": ".py",
225+
"mimetype": "text/x-python",
226+
"name": "python",
227+
"nbconvert_exporter": "python",
228+
"pygments_lexer": "ipython3",
229+
"version": "3.6.4"
230+
}
231+
},
232+
"nbformat": 4,
233+
"nbformat_minor": 2
234+
}

0 commit comments

Comments
 (0)