Last active
January 23, 2016 12:42
-
-
Save tchakravarty/744da3a6df1aeabe258a to your computer and use it in GitHub Desktop.
Python: Mutating objects created on-the-fly
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
# http://chat.stackoverflow.com/transcript/message/28317173#28317173 | |
# the list() invocation creates a list but that is not assigned to the | |
# local namespace. append() changes it but does not return the | |
# the original object which is now lost | |
bar = list(range(0,8)).append(23) | |
print(bar) | |
# this breaks down the creation and the mutation into two | |
foo = list(range(0,8)) | |
foo.append(23) | |
print(foo) | |
# to perform the creation and the mutation in one-go | |
bar = list(range(0,8)) + 23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment