Marko Rauhamaa wrote: > Mark Summerfield <l...@qtrac.plus.com>: > >> object() returns a minimal featureless object with no dictionary (no >> __dict__ attribute). This makes sense for efficiency since not all >> objects need a dictionary. > > __setattr__ could create __dict__ belatedly.
Are we designing Son Of PHP, or a sensible language? *wink* If object.__setattr__ did this, then we're left with two equally horrible choices: - only object works this way, not subclasses of object, which violates the Liskov substitution principle; - all instances, of every type, work this way, which causes chaos and destruction everywhere. Some of the consequences: * Classes with __slots__ will stop working as expected. * Immutable instances will no longer be immutable, since they can have mutable state. * The CPython interpreter shares immutable built-ins like ints across multiple processes. Either CPython would have to stop doing this (increasing memory requirements significantly), or one process could reach across to another and mutate its ints. Well, perhaps not *equally* horrible. Compared to the second alternative, violating the LSP seems relatively benign. There's also the implementation difficulty that instance.__dict__ is kept in a slot (obviously, since it cannot be stored in the per instance __dict__ !), which means that you cannot easily or practically expect to dynamically add/or delete it from instances. Now I'm sure with sufficient effort we could do so, but that's a lot of effort for negligible (or even negative) gain. -- Steven -- https://mail.python.org/mailman/listinfo/python-list