Created
July 8, 2019 14:43
-
-
Save guocheng/af050003453c73c2f71bf45b571337b0 to your computer and use it in GitHub Desktop.
Python is pass by value or by assignment
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" According to the offical documentation, Python is [pass by assignment](https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference).\n", | |
" It is not `pass by value` nor `pass by reference`. Let me explain:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"def modify(lst):\n", | |
" lst = [0,0,0]\n", | |
"\n", | |
"l = [1,2,3]\n", | |
"modify(l)\n", | |
"print(l)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" `lst` is a local variable inside `modify`. It is a copy of the variable `l` but not a copy of the list `[1,2,3]`.\n", | |
" If written in code, `lst` is:\n", | |
"```python\n", | |
"l = [1,2,3]\n", | |
"lst = l\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" You can modify `l` by:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 2, 3, 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"def modify_2(lst):\n", | |
" lst[0] = 0\n", | |
" lst.append(4)\n", | |
"\n", | |
"l = [1,2,3]\n", | |
"modify_2(l)\n", | |
"print(l)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Python is not pass by value\n", | |
" If this is the case, then `lst` is equivlent of:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"l = [1,2,3]\n", | |
"lst = l.copy() \n", | |
"lst[0] = 0 # any modification to lst does not affect l\n", | |
"print(l)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Python is not pass by reference\n", | |
" If this is the case, then:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"def modify(lst):\n", | |
" lst = [0,0,0]\n", | |
"\n", | |
"l = [1,2,3]\n", | |
"modify(l)\n", | |
"print(l) #if python is pass by reference, this will print [0,0,0]. But this is not the case." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## What is pass by assginment again?\n", | |
" It is marking a copy of a reference to an object.\n", | |
"```python\n", | |
"l = [1,2,3]\n", | |
"lst = l\n", | |
"```" | |
] | |
} | |
], | |
"metadata": { | |
"file_extension": ".py", | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"mimetype": "text/x-python", | |
"name": "python", | |
"npconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": 3 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment