Created
January 21, 2014 06:31
-
-
Save leepro/8535317 to your computer and use it in GitHub Desktop.
ipython notebook: Understanding frame of Python
This file contains 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": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Understanding 'frame' of Python / \n", | |
"\ud30c\uc774\uc36c \ud504\ub808\uc784 \uc774\ud574\ud558\uae30" | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"**\ud30c\uc774\uc36c\uc5d0\uc11c \ud604\uc7ac \uc2e4\ud589\uc758 \ucee8\ud14d\uc2a4\ud2b8\uc778 \ud504\ub808\uc784\uc758 \uc774\ud574\uc744 \uc608\uc81c\ub85c \uc0b4\ud3b4\ubcf4\uace0\uc790 \ud55c\ub2e4.**\n", | |
"\uc791\uc131: D. Lee" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import sys, dis" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 107 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"frames = []\n", | |
"def Two():\n", | |
" global frames\n", | |
" frames.append(sys._getframe())\n", | |
" abc = 0\n", | |
" abc+=1\n", | |
" frames.append(sys._getframe())\n", | |
" return abc\n", | |
"\n", | |
"def One():\n", | |
" global frames\n", | |
" frames.append(sys._getframe())\n", | |
" temp = Two() + 10\n", | |
" frames.append(sys._getframe())\n", | |
" return temp\n", | |
"\n", | |
"if __name__==\"__main__\":\n", | |
" frames.append(sys._getframe())\n", | |
" One()\n", | |
" frames.append(sys._getframe())" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 108 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"\uc2e4\ud589 \ud6c4 \ud504\ub808\uc784 \ub9ac\uc2a4\ud2b8" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"frames" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 109, | |
"text": [ | |
"[<frame at 0x1033aac60>,\n", | |
" <frame at 0x1033ab060>,\n", | |
" <frame at 0x1033aaa80>,\n", | |
" <frame at 0x1033aaa80>,\n", | |
" <frame at 0x1033ab060>,\n", | |
" <frame at 0x1033aac60>]" | |
] | |
} | |
], | |
"prompt_number": 109 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**\uc704\uc758 \ucf54\ub4dc\uac00 \uc2e4\ud589\ub41c \uc21c\uc11c\uc640 \ud504\ub808\uc784\uc758 \uc21c\uc11c\ub97c \ud655\uc778 \ud560 \uc218 \uc788\ub2e4.**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for f in frames:\n", | |
" print \"LINE %2d: Func: %-10s Address: %s\" % (f.f_lineno, f.f_code.co_name, hex(id(f)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"LINE 20: Func: <module> Address: 0x1033aac60\n", | |
"LINE 15: Func: One Address: 0x1033ab060\n", | |
"LINE 8: Func: Two Address: 0x1033aaa80\n", | |
"LINE 8: Func: Two Address: 0x1033aaa80\n", | |
"LINE 15: Func: One Address: 0x1033ab060\n", | |
"LINE 20: Func: <module> Address: 0x1033aac60\n" | |
] | |
} | |
], | |
"prompt_number": 110 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"\ud504\ub808\uc784\uc758 \ucf54\ub4dc \ud655\uc778\ud558\uae30" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**\ub450\ubc88\uc9f8 \ud504\ub808\uc784\uc740 One\uc774\ub77c\ub294 \ud568\uc218\uc5d0\uc11c \uc2e4\ud589\uc911\uc774\uba70 \uadf8 \ud504\ub808\uc784\uc758 \ubc14\uc774\ud2b8\ucf54\ub4dc\ub97c \ud655\uc778\ud574 \ubcf4\uc790.**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dis.dis(frames[1].f_code)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 12 0 LOAD_GLOBAL 0 (frames)\n", | |
" 3 LOAD_ATTR 1 (append)\n", | |
" 6 LOAD_GLOBAL 2 (sys)\n", | |
" 9 LOAD_ATTR 3 (_getframe)\n", | |
" 12 CALL_FUNCTION 0\n", | |
" 15 CALL_FUNCTION 1\n", | |
" 18 POP_TOP \n", | |
"\n", | |
" 13 19 LOAD_GLOBAL 4 (Two)\n", | |
" 22 CALL_FUNCTION 0\n", | |
" 25 LOAD_CONST 1 (10)\n", | |
" 28 BINARY_ADD \n", | |
" 29 STORE_FAST 0 (temp)\n", | |
"\n", | |
" 14 32 LOAD_GLOBAL 0 (frames)\n", | |
" 35 LOAD_ATTR 1 (append)\n", | |
" 38 LOAD_GLOBAL 2 (sys)\n", | |
" 41 LOAD_ATTR 3 (_getframe)\n", | |
" 44 CALL_FUNCTION 0\n", | |
" 47 CALL_FUNCTION 1\n", | |
" 50 POP_TOP \n", | |
"\n", | |
" 15 51 LOAD_FAST 0 (temp)\n", | |
" 54 RETURN_VALUE \n" | |
] | |
} | |
], | |
"prompt_number": 111 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"END OF PYTHON" | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment