|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 |
| - "celltoolbar": "Raw Cell Format", |
4 | 3 | "name": "",
|
5 |
| - "signature": "sha256:b7fafba0fc82a59cb7cf338ba66c116c70894fd255717b655cf92f97673c9043" |
| 4 | + "signature": "sha256:9508ad401da191ade544e7d349ad8f99b7ecf0fdcfb71d8696001b5a26a47e70" |
6 | 5 | },
|
7 | 6 | "nbformat": 3,
|
8 | 7 | "nbformat_minor": 0,
|
|
79 | 78 | "\n",
|
80 | 79 | "func()\n",
|
81 | 80 | "\n",
|
82 |
| - "type(func)" |
| 81 | + "print(type(func))" |
83 | 82 | ],
|
84 | 83 | "language": "python",
|
85 | 84 | "metadata": {},
|
|
90 | 89 | "text": [
|
91 | 90 | "Before func() is called.\n",
|
92 | 91 | "Behavior from func().\n",
|
93 |
| - "After func() is called.\n" |
94 |
| - ] |
95 |
| - }, |
96 |
| - { |
97 |
| - "metadata": {}, |
98 |
| - "output_type": "pyout", |
99 |
| - "prompt_number": 2, |
100 |
| - "text": [ |
101 |
| - "function" |
| 92 | + "After func() is called.\n", |
| 93 | + "<class 'function'>\n" |
102 | 94 | ]
|
103 | 95 | }
|
104 | 96 | ],
|
105 |
| - "prompt_number": 2 |
| 97 | + "prompt_number": 3 |
106 | 98 | },
|
107 | 99 | {
|
108 | 100 | "cell_type": "markdown",
|
|
144 | 136 | ]
|
145 | 137 | }
|
146 | 138 | ],
|
147 |
| - "prompt_number": 9 |
| 139 | + "prompt_number": 1 |
148 | 140 | },
|
149 | 141 | {
|
150 | 142 | "cell_type": "markdown",
|
|
168 | 160 | " self.f()\n",
|
169 | 161 | " print(\"After {function_name}() is called.\".format(function_name=self.f.__name__))\n",
|
170 | 162 | " \n",
|
171 |
| - "@DecoratorCls\n", |
172 | 163 | "def func():\n",
|
173 | 164 | " print(\"Behavior from func().\")\n",
|
174 | 165 | " \n",
|
175 |
| - "func()" |
| 166 | + "func = DecoratorCls(func)\n", |
| 167 | + "\n", |
| 168 | + "func()\n", |
| 169 | + "\n", |
| 170 | + "print(type(func))" |
176 | 171 | ],
|
177 | 172 | "language": "python",
|
178 | 173 | "metadata": {},
|
|
183 | 178 | "text": [
|
184 | 179 | "Before func() is called.\n",
|
185 | 180 | "Behavior from func().\n",
|
186 |
| - "After func() is called.\n" |
| 181 | + "After func() is called.\n", |
| 182 | + "<class '__main__.DecoratorCls'>\n" |
187 | 183 | ]
|
188 | 184 | }
|
189 | 185 | ],
|
190 |
| - "prompt_number": 4 |
| 186 | + "prompt_number": 2 |
191 | 187 | },
|
192 | 188 | {
|
193 | 189 | "cell_type": "markdown",
|
194 | 190 | "metadata": {},
|
195 | 191 | "source": [
|
196 |
| - "## So, Python Decorators are callable objects that modify the behavior of other callable objects." |
| 192 | + "#### Again, this is equivalent to the code above." |
197 | 193 | ]
|
198 | 194 | },
|
199 | 195 | {
|
200 | 196 | "cell_type": "code",
|
201 | 197 | "collapsed": false,
|
202 |
| - "input": [], |
| 198 | + "input": [ |
| 199 | + "class DecoratorCls(object):\n", |
| 200 | + " def __init__(self, f):\n", |
| 201 | + " self.f = f\n", |
| 202 | + " \n", |
| 203 | + " def __call__(self):\n", |
| 204 | + " print(\"Before {function_name}() is called.\".format(function_name=self.f.__name__))\n", |
| 205 | + " self.f()\n", |
| 206 | + " print(\"After {function_name}() is called.\".format(function_name=self.f.__name__))\n", |
| 207 | + " \n", |
| 208 | + "@DecoratorCls\n", |
| 209 | + "def func():\n", |
| 210 | + " print(\"Behavior from func().\")\n", |
| 211 | + " \n", |
| 212 | + "func()\n", |
| 213 | + "\n", |
| 214 | + "print(type(func))" |
| 215 | + ], |
203 | 216 | "language": "python",
|
204 | 217 | "metadata": {},
|
205 |
| - "outputs": [] |
| 218 | + "outputs": [ |
| 219 | + { |
| 220 | + "output_type": "stream", |
| 221 | + "stream": "stdout", |
| 222 | + "text": [ |
| 223 | + "Before func() is called.\n", |
| 224 | + "Behavior from func().\n", |
| 225 | + "After func() is called.\n", |
| 226 | + "<class '__main__.DecoratorCls'>\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
| 230 | + "prompt_number": 4 |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "markdown", |
| 234 | + "metadata": {}, |
| 235 | + "source": [ |
| 236 | + "## So, Python Decorators are callable objects that modify the behavior of other callable objects." |
| 237 | + ] |
206 | 238 | }
|
207 | 239 | ],
|
208 | 240 | "metadata": {}
|
|
0 commit comments