On 1/11/21 4:39 PM, Guido van Rossum wrote:
The easiest thing would be just to create an empty `__annotations__` for classes that have no annotated variables, and to hell with the cost.I assume you'd keep the existing behavior where functions lazy-create an empty dict if they have no annotations too?
That all would work fine and be consistent, but you'd probably have to set the empty __annotations__ dict on modules too. I've noticed that code that examines annotations tends to handle two classes of objects: "functions" and "not-functions". Modules also store their __annotations__ in their __dict__, so the same code path works fine for examining the annotations of both classes and modules.
(I noticed that `__slots__` is missing from your list. Maybe because it follows yet another pattern?)
I forgot about __slots__! Yup, it's optional, and you can even delete it, though after the class is defined I'm not sure how much difference that makes.
Slots intelligently support inheritance, too. I always kind of wondered why annotations didn't support inheritance--if D is a subclass of C, why doesn't D.__annotations__ contain all C's annotations too? But we're way past reconsidering that behavior now.