Created
October 27, 2018 01:47
-
-
Save oversider-kosma/ce85680aab61eb206df04c6dfe08b06a to your computer and use it in GitHub Desktop.
Multiple assignment and dict
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
#!/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