Skip to content

Instantly share code, notes, and snippets.

@capttwinky
Created May 9, 2014 17:35
Show Gist options
  • Save capttwinky/2aefbb1c9e8e113b04e2 to your computer and use it in GitHub Desktop.
Save capttwinky/2aefbb1c9e8e113b04e2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import dis\n",
"\n",
"foo = [1]\n",
"\n",
"def bar(thing):\n",
" foo.append(thing)\n",
"\n",
"def baz(thing):\n",
" foo += thing\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bar(2)\n",
"bar(3)\n",
"print foo"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1, 2, 3]\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"baz('crap')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'foo' referenced before assignment",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-4-c100cede7033>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbaz\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'crap'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-1-ffa6c9e26809>\u001b[0m in \u001b[0;36mbaz\u001b[1;34m(thing)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mbaz\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mfoo\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mthing\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'foo' referenced before assignment"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dis.dis(bar)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 6 0 LOAD_GLOBAL 0 (foo)\n",
" 3 LOAD_ATTR 1 (append)\n",
" 6 LOAD_FAST 0 (thing)\n",
" 9 CALL_FUNCTION 1\n",
" 12 POP_TOP \n",
" 13 LOAD_CONST 0 (None)\n",
" 16 RETURN_VALUE \n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dis.dis(baz)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 9 0 LOAD_FAST 1 (foo)\n",
" 3 LOAD_FAST 0 (thing)\n",
" 6 INPLACE_ADD \n",
" 7 STORE_FAST 1 (foo)\n",
" 10 LOAD_CONST 0 (None)\n",
" 13 RETURN_VALUE \n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def baz_plus(thing):\n",
" global foo\n",
" foo += thing"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"baz_plus('crap')\n",
"print foo"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1, 2, 3, 'c', 'r', 'a', 'p']\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dis.dis(baz_plus)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 3 0 LOAD_GLOBAL 0 (foo)\n",
" 3 LOAD_FAST 0 (thing)\n",
" 6 INPLACE_ADD \n",
" 7 STORE_GLOBAL 0 (foo)\n",
" 10 LOAD_CONST 0 (None)\n",
" 13 RETURN_VALUE \n"
]
}
],
"prompt_number": 9
},
{
"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