by iknownothow · 4y ago · 0 comments
Ya, this is because the initialized object is stored as the default parameter, and is not initialized every time the function runs.
def functy(dicty={}):
print('dicty=', dicty)
return dicty
output = functy()
# dicty = {}
output.update({'key': 'value'})
functy()
# dicty = {'key': 'value'}I think this behavior is actually "default args" but reversed. In case of default args it is the mutability that causes the nasty surprise (ooops, all calls share the same object, which is mutable). In case of the "+=" it is the _immutability_ that causes the surprise - I would not expect "obj.a += ..." to create an instance attribute that shadows the class one.
I think the practical conclusion here is "don't call += on objects that don't support it - because it may work and this is not what you want!)".
I'm curious if you see it the same way?
It seems to, but, actually, it doesn't.
> but wtf'ingly also alters the value of the class attribute "A.b" which completely alters the behavior of A.
Actually, all it does is call the __iadd__ method on the list that it is the value of A.b (which modifies that list in-place.)
The value of A.b can, incidentally be accessed via obj.b because obj.b does not exist and the lookup path for instance attributes includes the class.
There are two things going on here there are potential sources of confusion, because they behavior differently in different circumstances: object member access (which can get members from the object or, if they don't exist on the object, from the class) and the += operator, which calls the __iadd__ method on the left value or acts as add and then assign to the name on the left, if that name doesn't reference a value that supports __iadd__.
That is not correct. += will always perform the assignment, hence the classic oddball concatenation:
>>> v = ([],)
>>> v
([],)
>>> v[0] += [1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> v
([1],)
So `obj.b += [2]` desugars to something along the lines of: l = obj.b
l.extend(2)
obj.b = l
Hence if you call vars() on the instance or access its __dict__, you will see that the instance does have an intrinsic "b" attribute: >>> class A: b = [1]
...
>>> a = A()
>>> a.b += (2,)
>>> vars(a), a.__dict__
({'b': [1, 2]}, {'b': [1, 2]})
And if you re-set the attribute on the class, it won't affect the instance, which it would if the instance delegated to the class: >>> A.b = [5]
>>> a.b
[1, 2]Side note: the object can be immutable, but then the (variable or attribute) left side should be replaced with a new immutable object.
I thought of this as though the list A.b was marked static - then any modification of b through any instance of A modifies the shared static member
>>> class A:
... b = [1,]
...
>>> obj = A()
>>> obj.b.append(2)
>>> A.b
[1, 2]
>>> foo = A()
>>> foo.b
[1, 2]
>>>It necessarily alters the value of A.b, since `+=` is specifically overridden to update the list in-place. With lists, `a += b` is specifically not an alias for `a = a + b`, instead its behaviour is closer to
a.extend(b)
a = a
The issue here is the first part, I absolutely hate this override, and sadly the core team has not learned a thing there as `|=` was overridden the exact same way on dicts when `dict | dict` was added to the language.Note to readers unfamiliar with Python: class attributes is a special thing, different than instance attributes.
Definitely one of the trickier things with Python for sure.