← Back to context

Comment by dumah

1 day ago

That’s not “the normal way”, it’s just the case when the LHS is immutable.

This behavior is congruent to C++ and it’s documented and idiomatic.

It’d be weird if the in-place operator deeply copied the LHS, and problematic for a garbage-collected language in high throughput applications.

> That’s not “the normal way”, it’s just the case when the LHS is immutable.

It’s the default behaviour of |= if you override |, you need to specifically override |= to do otherwise.

> This behavior is congruent to C++

An excellent reason to do something else in and of itself.

> it’s documented and idiomatic.

Documenting a trap does not make it less of a trap. Innocently replacing | by |= induces a massive behavioural change which the user has little reason to expect, and which can easily lead to bugs due to mutating shared data.

> It’d be weird if the in-place operator deeply copied the LHS

I see you understand neither Python nor the behaviour of the | operator.

> problematic for a garbage-collected language in high throughput applications.

dict.update exists, if you specifically need to update the LHS in place it works just fine.