Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created December 28, 2015 05:16
Show Gist options
  • Save rtoal/a1fe1bd18a73528009db to your computer and use it in GitHub Desktop.
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
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