# mutable_args.py David MacQuigg ece175 5/7/08 ''' How to make primitive values mutable in Python. You can wrap a primitive value in a mutable object, and modify the value via attributes of the object. A list [] is a convenient mutable object. ''' def f(a, b): print "a = %s b = %s" % (a, b) a += 1 # a now points to a local object. b[0] += 10 # b still points to the original object b.append('xyz') print "a = %s b = %s" % (a, b) def main(): ''' >>> main() x = 0 y = [0] a = 0 b = [0] a = 1 b = [10, 'xyz'] x = 0 y = [10, 'xyz'] ''' x = 0; y = [0] # a simple integer, and wrapped integer print "x = %s y = %s" % (x, y) f(x, y) print "x = %s y = %s" % (x, y) from doctest import testmod testmod(verbose=True)