Mark Shannon's presentation at the 2017 Language Summit · Issue #432 · python/typing (original) (raw)
Mark observed that the typing module uses classes to represent types. This can be expensive, since e.g. the type List[int] really ought to be the tuple (List, int) but it's actually a class object which has a fair amount of overhead (though not as much as early versions of typing.py, since we now cache these class objects).
If we changed to tuples (or at least objects simpler than class object), we'd have a problem: The simpler object couldn't be subclassed from. But who subclasses List[int]? Then again, maybe simpler objects aren't the point?
Mark also pointed out that after
from typing import List class C(List[int]): pass print(C.mro)
We find that C.__mro__
has 17 items!
I confirmed this. The roughly equivalent code using collections.abc
from collections.abc import MutableMapping class C(MutableMapping): pass print(C.mro)
has only 7 items. And subclassing builtins.list
class C(list): pass print(C.mro)
has only three.
This affects performance, e.g. which is faster?
class C(list, Sequence[int]): pass C().append(1) class D(Sequence[int], list): pass D().append(1)
One append() call is 10% faster than the other.