Skip to content

Instantly share code, notes, and snippets.

@tchakravarty
Last active January 23, 2016 12:42
Show Gist options
  • Save tchakravarty/744da3a6df1aeabe258a to your computer and use it in GitHub Desktop.
Save tchakravarty/744da3a6df1aeabe258a to your computer and use it in GitHub Desktop.
Python: Mutating objects created on-the-fly
# 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