I have this piece of code in python which I fail to understand as how are these functions maintain their state when encapsulated in class.
class Param(object):
def bad_append(self, item, l=[]):
l.append(item)
return l
p = Param()
print(p.bad_append(1))
print(p.bad_append(2))
pp = Param()
print(pp.bad_append(100))
Output
[1]
[1, 2]
[1, 2, 100]
I can't understand as why the output for pp is [1, 2, 100]
instead of [100]
.