I have found strange Python's behavior (or probably I don't understand how inheritance and/or default values of attributes works).
For given code
class A(object):
def __init__(self, s):
self.s = s
print "in init", self.s
class B(A):
def __init__(self, s = set()):
super(B, self).__init__(s)
print "after super", self.s
self.s.add('foo')
print '--------------'
if __name__ == "__main__":
a = B()
b = B()
I get following output:
in init set([])
after super set([])
--------------
in init set(['foo']) # Why it has value set in other object?!
after super set(['foo'])
--------------
Of course desired behavior would be to init self.s in second object (b) with an empty set, but for unknown reason it gets state from previous object. Why does it happen? How to obtain desired behavior?
Thanks!