Created
April 9, 2013 06:22
-
-
Save chaobin/5343381 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Execution order of decorators" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def deco_a(func):\n", | |
" def wrapper(*args, **kwargs):\n", | |
" print \"decoration a running\"\n", | |
" return func(*args, **kwargs)\n", | |
" return wrapper\n", | |
"\n", | |
"def deco_b(func):\n", | |
" def wrapper(*args, **kwargs):\n", | |
" print \"decoration b running\"\n", | |
" return func(*args, **kwargs)\n", | |
" return wrapper" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"@deco_a\n", | |
"@deco_b\n", | |
"def func():\n", | |
" print 'f called'\n", | |
"\n", | |
"func()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"decoration a running\n", | |
"decoration b running\n", | |
"f called\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import time\n", | |
"\n", | |
"def wait(func):\n", | |
" def wrapper(*args, **kwargs):\n", | |
" print 'waiting'\n", | |
" time.sleep(2)\n", | |
" return func(*args, **kwargs)\n", | |
" return wrapper\n", | |
"\n", | |
"@wait\n", | |
"@deco_a\n", | |
"def func():\n", | |
" print 'f called'\n", | |
"\n", | |
"func()\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"waiting\n", | |
"decoration a running" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"f called\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"func()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"waiting\n", | |
"decoration a running" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"f called\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def deco_a(func):\n", | |
" def wrapper(*args, **kwargs):\n", | |
" result = func(*args, **kwargs)\n", | |
" print \"decoration a running\"\n", | |
" return result\n", | |
" return wrapper\n", | |
"\n", | |
"def deco_b(func):\n", | |
" def wrapper(*args, **kwargs):\n", | |
" result = func(*args, **kwargs)\n", | |
" print \"decoration b running\"\n", | |
" return result\n", | |
" return wrapper" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"@deco_a\n", | |
"@deco_b\n", | |
"def func():\n", | |
" print 'f called'\n", | |
"func()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"f called\n", | |
"decoration b running\n", | |
"decoration a running\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment