3 ms·
Python's data structures (with few exceptions) are completely mutable, which means any function can add, modify, or even remove attributes from some object at a
by yen223 9y ago
Python's data structures (with few exceptions) are completely mutable, which means any function can add, modify, or even remove attributes from some object at any time.
Look at Django's request object for example. Django, by convention, adds a bunch of attributes to the request object at runtime, via middlewares. This is a problem because a dev can't reason about the shape of request object, without stepping through all the middlewares and figuring out which is doing what.
We could use immutable classes internally, but that doesn't solve the problem that external libraries (like Django!) abuse them all over the place.
- jordic 9y agoIf you really need this you can use zope.interface to enforce class immutability..