Skip to content

Instantly share code, notes, and snippets.

@cd34
Last active December 19, 2015 03:48
Show Gist options
  • Save cd34/5892230 to your computer and use it in GitHub Desktop.
Save cd34/5892230 to your computer and use it in GitHub Desktop.
mutable demonstration code
#!/usr/bin/python
import copy
def mutable(myset):
myset.extend([10,11,12])
print 'inside mutable function:', myset
def nonmutable(myset):
myset = copy.copy(myset)
myset.extend([10,11,12])
print 'inside nonmutable function:', myset
print
print 'Mutable test'
print
ourset = [1, 2, 3, 4, 5, 6]
print 'Our set before calling:', ourset
mutable(ourset)
print 'Our set after calling:', ourset
print
print 'NonMutable test'
print
ourset = [1, 2, 3, 4, 5, 6]
print 'Our set before calling:', ourset
nonmutable(ourset)
print 'Our set after calling:', ourset
$ python mutable.py
Mutable test
Our set before calling: [1, 2, 3, 4, 5, 6]
inside mutable function: [1, 2, 3, 4, 5, 6, 10, 11, 12]
Our set after calling: [1, 2, 3, 4, 5, 6, 10, 11, 12]
NonMutable test
Our set before calling: [1, 2, 3, 4, 5, 6]
inside nonmutable function: [1, 2, 3, 4, 5, 6, 10, 11, 12]
Our set after calling: [1, 2, 3, 4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment