27 ms·
Guido used to be hostile to __slots__, but apparently he's lightened up. If you're not using "setattr" to dynamically add fields to your object, CPython's unde
by Animats 9y ago
Guido used to be hostile to __slots__, but apparently he's lightened up.
If you're not using "setattr" to dynamically add fields to your object, CPython's underlying machinery is overkill. Everything is a dict, and all accesses take a lookup. With __slots__, a Python object is more like a struct in other languages.
Slotted objects should be the default. You should have to inherit from "DynamicObject" or something like it to enable setattr on an object. Actually, dynamic objects stopped being that important once Python let you inherit from "dict". Then, if you wanted "foo[ix]", it was easy to do it.
There are some packages, such as BeautifulSoup, which use dynamic attributes heavily. BeautifulSoup uses it to map HTML tags to attributes.
This is more trouble than it's worth, because when an HTML tag name clashes with a Python builtin name, workarounds are necessary.
- make3 9y ago> "should" this would make python a different language completely
- Spivak 9y agoI mean it would certainly be a breaking change to existing code, and we know how well that goes over in the Python community, but it wouldn't be a completely different language. The argument is that these objects are a sane default that would prevent a lot of accidental programming mistakes and I agree.
- vidarh 9y agoSee my comment elsewhere about a "split" approach to this for a Ruby implementation. I get that there's a semantic difference to change from __dict__ to the way __slots__ currently works, but it might be feasible to change the way __dict__ works to inline instance variables in the instance by default in cases where they're readily statically inferred and get much of the same effect without changing the semantics of __dict__.