Created
December 28, 2015 05:16
-
-
Save rtoal/a1fe1bd18a73528009db to your computer and use it in GitHub Desktop.
Determines whether Python is call-by-value, call-by-reference, or call-by-sharing
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
arg = {'x': 0} | |
def f(param): | |
same_ids = id(arg) == id(param) | |
different_ids = not same_ids | |
param = {'y': 4} | |
arg_changed = 'y' in arg | |
print('Call by value: {}'.format(different_ids)) | |
print('Call by sharing: {}'.format(same_ids and not arg_changed)) | |
print('Call by reference: {}'.format(same_ids and arg_changed)) | |
f(arg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment