|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "### 静态方法和类成员方法分别在创建时分别被装入staticmethod类型和classmethod类型的对象中。\n", |
| 7 | + "**静态方法和类成员方法分别在创建时分别被装入staticmethod类型和classmethod类型的对象中。**\n", |
8 | 8 | "\n", |
9 | | - "### 静态方法的定义没有self参数,且能够被类本身直接调用。\n", |
| 9 | + "**静态方法的定义没有self参数,且能够被类本身直接调用。**\n", |
10 | 10 | "\n", |
11 | 11 | "\n", |
12 | | - "### 类方法在定义时需要名为cls的类似于self的参数,类成员方法可以直接用类的具体对象调用。但cls参数时自动被绑定到类的。" |
| 12 | + "**类方法在定义时需要名为cls的类似于self的参数,类成员方法可以直接用类的具体对象调用。但cls参数时自动被绑定到类的。**" |
13 | 13 | ] |
14 | 14 | }, |
15 | 15 | { |
|
68 | 68 | "source": [ |
69 | 69 | "手动包装和替换优点蠢!为这样的包装方法引入了一个叫装饰器的新语法。\n", |
70 | 70 | "\n", |
71 | | - "### 装饰器能对任何可调用的对象进行包装,既能够用于方法也能用于函数。" |
| 71 | + "**装饰器能对任何可调用的对象进行包装,既能够用于方法也能用于函数。**" |
72 | 72 | ] |
73 | 73 | }, |
74 | 74 | { |
75 | 75 | "cell_type": "markdown", |
76 | 76 | "metadata": {}, |
77 | 77 | "source": [ |
78 | | - "### 使用@操作符,在方法(函数)的上方将装饰器列出,从而指定一个或者更多的装饰器(多个装饰器在应用时的顺序与指定顺序相反)。" |
| 78 | + "**使用`@`操作符,在方法(函数)的上方将装饰器列出,从而指定一个或者更多的装饰器(多个装饰器在应用时的顺序与指定顺序相反)。**" |
79 | 79 | ] |
80 | 80 | }, |
81 | 81 | { |
|
154 | 154 | "cell_type": "markdown", |
155 | 155 | "metadata": {}, |
156 | 156 | "source": [ |
157 | | - "##### @decorator(dec_opt_args)\n", |
158 | | - "##### def fun2Bdecorator(func_opt_args):\n", |
159 | | - " ...." |
| 157 | + "```python\n", |
| 158 | + "@decorator(dec_opt_args)\n", |
| 159 | + "def fun2Bdecorator(func_opt_args):\n", |
| 160 | + " pass\n", |
| 161 | + "```" |
160 | 162 | ] |
161 | 163 | }, |
162 | 164 | { |
|
170 | 172 | "cell_type": "markdown", |
171 | 173 | "metadata": {}, |
172 | 174 | "source": [ |
173 | | - "##### @deco2\n", |
174 | | - "##### @deco1\n", |
175 | | - "##### def(arg1, arg2, ...):\n", |
176 | | - " pass" |
| 175 | + "```python\n", |
| 176 | + "@deco2\n", |
| 177 | + "@deco1\n", |
| 178 | + "def(arg1, arg2[,..]):\n", |
| 179 | + " pass\n", |
| 180 | + "```" |
177 | 181 | ] |
178 | 182 | }, |
179 | 183 | { |
|
187 | 191 | "cell_type": "markdown", |
188 | 192 | "metadata": {}, |
189 | 193 | "source": [ |
190 | | - "##### def func(arg1, arg2, ...):\n", |
191 | | - " pass\n", |
192 | | - "##### func = deco2(deco1(func))" |
| 194 | + "```python\n", |
| 195 | + "def func(arg1, arg2, ...):\n", |
| 196 | + " pass\n", |
| 197 | + "```\n", |
| 198 | + "```python\n", |
| 199 | + "func = deco2(deco1(func))\n", |
| 200 | + "```" |
193 | 201 | ] |
194 | 202 | }, |
195 | 203 | { |
|
205 | 213 | "cell_type": "markdown", |
206 | 214 | "metadata": {}, |
207 | 215 | "source": [ |
208 | | - "##### @g\n", |
209 | | - "##### @f\n", |
210 | | - "##### def foo():\n", |
| 216 | + "```python\n", |
| 217 | + "@g\n", |
| 218 | + "@f\n", |
| 219 | + "def foo():\n", |
211 | 220 | " pass\n", |
212 | | - "##### 与foo = g(f(foo))相同" |
| 221 | + "```\n", |
| 222 | + "与\n", |
| 223 | + "```python\n", |
| 224 | + "foo = g(f(foo))\n", |
| 225 | + "```\n", |
| 226 | + "相同" |
213 | 227 | ] |
214 | 228 | }, |
215 | 229 | { |
|
223 | 237 | "cell_type": "markdown", |
224 | 238 | "metadata": {}, |
225 | 239 | "source": [ |
226 | | - "#### @deco\n", |
227 | | - "#### def foo(): pass" |
| 240 | + "```python\n", |
| 241 | + "@deco\n", |
| 242 | + "def foo():\n", |
| 243 | + " pass\n", |
| 244 | + "```" |
228 | 245 | ] |
229 | 246 | }, |
230 | 247 | { |
|
238 | 255 | "cell_type": "markdown", |
239 | 256 | "metadata": {}, |
240 | 257 | "source": [ |
241 | | - "#### @decomaker(deco_args)\n", |
242 | | - "#### def foo(): pass" |
| 258 | + "```python\n", |
| 259 | + "@decomaker(deco_args)\n", |
| 260 | + "def foo():\n", |
| 261 | + " pass\n", |
| 262 | + "```" |
243 | 263 | ] |
244 | 264 | }, |
245 | 265 | { |
|
253 | 273 | "cell_type": "markdown", |
254 | 274 | "metadata": {}, |
255 | 275 | "source": [ |
256 | | - "#### foo = decomaker(deco_args)(foo)" |
| 276 | + "```python\n", |
| 277 | + "foo = decomaker(deco_args)(foo)\n", |
| 278 | + "```" |
257 | 279 | ] |
258 | 280 | }, |
259 | 281 | { |
|
267 | 289 | "cell_type": "markdown", |
268 | 290 | "metadata": {}, |
269 | 291 | "source": [ |
270 | | - "#### @deco1(deco_arg)\n", |
271 | | - "#### @deco2\n", |
272 | | - "#### def func(): pass" |
| 292 | + "```python\n", |
| 293 | + "@deco1(deco_arg)\n", |
| 294 | + "@deco2\n", |
| 295 | + "def func():\n", |
| 296 | + " pass\n", |
| 297 | + "```" |
273 | 298 | ] |
274 | 299 | }, |
275 | 300 | { |
|
290 | 315 | "cell_type": "markdown", |
291 | 316 | "metadata": {}, |
292 | 317 | "source": [ |
293 | | - "#### 引入日志" |
294 | | - ] |
295 | | - }, |
296 | | - { |
297 | | - "cell_type": "markdown", |
298 | | - "metadata": {}, |
299 | | - "source": [ |
300 | | - "#### 增加计逻辑来检测性能" |
301 | | - ] |
302 | | - }, |
303 | | - { |
304 | | - "cell_type": "markdown", |
305 | | - "metadata": {}, |
306 | | - "source": [ |
307 | | - "#### 给函数加入事务的能力" |
308 | | - ] |
309 | | - }, |
310 | | - { |
311 | | - "cell_type": "markdown", |
312 | | - "metadata": {}, |
313 | | - "source": [ |
| 318 | + "+ 引入日志\n", |
| 319 | + "\n", |
| 320 | + "+ 增加计逻辑来检测性能\n", |
| 321 | + "\n", |
| 322 | + "+ 给函数加入事务的能力\n", |
| 323 | + "\n", |
314 | 324 | "对于用python创建企业级应用,支持装饰器的特性是非常重要的。" |
315 | 325 | ] |
316 | 326 | }, |
317 | 327 | { |
318 | 328 | "cell_type": "markdown", |
319 | 329 | "metadata": {}, |
320 | 330 | "source": [ |
321 | | - "## 装饰器本质上是一个python函数, 可以让其他函数再不需要做任何代码变动的前提下增加额外功能。即装饰器的作用就是为已经存在的对象添加额外的功能。" |
| 331 | + "**装饰器本质上是一个python函数, 可以让其他函数再不需要做任何代码变动的前提下增加额外功能。即装饰器的作用就是为已经存在的对象添加额外的功能。**" |
322 | 332 | ] |
323 | | - }, |
324 | | - { |
325 | | - "cell_type": "code", |
326 | | - "execution_count": null, |
327 | | - "metadata": { |
328 | | - "collapsed": true |
329 | | - }, |
330 | | - "outputs": [], |
331 | | - "source": [] |
332 | 333 | } |
333 | 334 | ], |
334 | 335 | "metadata": { |
| 336 | + "hide_input": false, |
335 | 337 | "kernelspec": { |
336 | | - "display_name": "Python 2", |
| 338 | + "display_name": "Python 3", |
337 | 339 | "language": "python", |
338 | | - "name": "python2" |
| 340 | + "name": "python3" |
339 | 341 | }, |
340 | 342 | "language_info": { |
341 | 343 | "codemirror_mode": { |
342 | 344 | "name": "ipython", |
343 | | - "version": 2 |
| 345 | + "version": 3 |
344 | 346 | }, |
345 | 347 | "file_extension": ".py", |
346 | 348 | "mimetype": "text/x-python", |
347 | 349 | "name": "python", |
348 | 350 | "nbconvert_exporter": "python", |
349 | | - "pygments_lexer": "ipython2", |
350 | | - "version": "2.7.13" |
| 351 | + "pygments_lexer": "ipython3", |
| 352 | + "version": "3.6.8" |
| 353 | + }, |
| 354 | + "nbTranslate": { |
| 355 | + "displayLangs": [ |
| 356 | + "*" |
| 357 | + ], |
| 358 | + "hotkey": "alt-t", |
| 359 | + "langInMainMenu": true, |
| 360 | + "sourceLang": "en", |
| 361 | + "targetLang": "fr", |
| 362 | + "useGoogleTranslate": true |
| 363 | + }, |
| 364 | + "toc": { |
| 365 | + "base_numbering": 1, |
| 366 | + "nav_menu": {}, |
| 367 | + "number_sections": true, |
| 368 | + "sideBar": true, |
| 369 | + "skip_h1_title": false, |
| 370 | + "title_cell": "Table of Contents", |
| 371 | + "title_sidebar": "Contents", |
| 372 | + "toc_cell": false, |
| 373 | + "toc_position": {}, |
| 374 | + "toc_section_display": true, |
| 375 | + "toc_window_display": false |
| 376 | + }, |
| 377 | + "varInspector": { |
| 378 | + "cols": { |
| 379 | + "lenName": 16, |
| 380 | + "lenType": 16, |
| 381 | + "lenVar": 40 |
| 382 | + }, |
| 383 | + "kernels_config": { |
| 384 | + "python": { |
| 385 | + "delete_cmd_postfix": "", |
| 386 | + "delete_cmd_prefix": "del ", |
| 387 | + "library": "var_list.py", |
| 388 | + "varRefreshCmd": "print(var_dic_list())" |
| 389 | + }, |
| 390 | + "r": { |
| 391 | + "delete_cmd_postfix": ") ", |
| 392 | + "delete_cmd_prefix": "rm(", |
| 393 | + "library": "var_list.r", |
| 394 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 395 | + } |
| 396 | + }, |
| 397 | + "types_to_exclude": [ |
| 398 | + "module", |
| 399 | + "function", |
| 400 | + "builtin_function_or_method", |
| 401 | + "instance", |
| 402 | + "_Feature" |
| 403 | + ], |
| 404 | + "window_display": false |
351 | 405 | } |
352 | 406 | }, |
353 | 407 | "nbformat": 4, |
|
0 commit comments