How does __slots__ avoid a dictionary lookup?
问题 I've heard that __slots__ makes objects faster by avoiding a dictionary lookup. My confusion comes from Python being a dynamic language. In a static language, we avoid a dictionary lookup for a.test by doing a compile-time optimisation to save the index in the instruction we run. Now, in Python, a could just as easily be another object that has a dictionary or a different set of attributes. It seems like we'll still have to do a dictionary lookup - the only difference seems to be that we only