Skip to content

Instantly share code, notes, and snippets.

@oversider-kosma
Created October 27, 2018 01:47
Show Gist options
  • Save oversider-kosma/ce85680aab61eb206df04c6dfe08b06a to your computer and use it in GitHub Desktop.
Save oversider-kosma/ce85680aab61eb206df04c6dfe08b06a to your computer and use it in GitHub Desktop.
Multiple assignment and dict
#!/usr/bin/env python
def key_first():
dct = {}
key = 'foo'
key, dct[key] = 'some_new_key', 'bar';
return dct
def dict_first():
dct = {}
key = 'foo'
dct[key], key = 'bar', 'some_new_key';
return dct
assert key_first().keys() == dict_first().keys() # AssertionError
# Because multiple assignment is not atomic
# In `key_first` when we assigning value by `key` it already has been changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment