How to modify a variable inside of function
Code example
class z(): def __init__(self): self.z = ['foo'] print 'before', self.z def zoo(self, doo): doo[0] = 'ya' class b(): def __init__(self): self.z = 'foo' print 'before', self.z def zoo(self, doo): doo = 'ya' A = z() A.zoo(A.z) print 'after', A.z print B = b() B.zoo(B.z) print 'after', B.z
Output [dandelion@bart ~]$ python z.py before ['foo'] after ['ya'] before foo after foo [dandelion@bart ~]$
Explanation
It’s simple. In Python ‘string’ is immutable object, while [list] is mutable one
.