Last active
December 19, 2015 03:48
-
-
Save cd34/5892230 to your computer and use it in GitHub Desktop.
mutable demonstration code
This file contains hidden or 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/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 'Mutable test' | |
ourset = [1, 2, 3, 4, 5, 6] | |
print 'Our set before calling:', ourset | |
mutable(ourset) | |
print 'Our set after calling:', ourset | |
print 'NonMutable test' | |
ourset = [1, 2, 3, 4, 5, 6] | |
print 'Our set before calling:', ourset | |
nonmutable(ourset) | |
print 'Our set after calling:', ourset |
This file contains hidden or 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
$ 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