On 1/11/21 5:05 PM, Greg Ewing wrote:
On 12/01/21 6:22 am, Larry Hastings wrote:
* The language will set __annotations__ to a dict if the object has
annotations, or None if it has no annotations.
That sounds inconvenient -- it means that any code referencing __annotations__ has to guard against the possibility of it being None.
It was a balancing act. Using an 64-byte empty dict per object with no defined annotations seems so wasteful. And anything short of an empty dict, you'd have to guard against. Current code already has to guard against "__annotations__ aren't set" anyway, so I figured the cost of migrating to checking a different condition would be small. And None is so cheap, and the guard is so easy:
If we're changing things, I'm wondering if the best thing would be to introduce an annotations() function as the new best practice for getting an object's annotations. It would know how to handle all the type-specific pecularities, and could take care of things such as manufacturing an empty dict if the object doesn't have any annotations.
I guess I'm marginally against this, just because it seems like a needless change. We don't need the flexibility of a function with optional parameters and such, and with a data descriptor we can already put code behind __annotations__ (as I have already done). Plus, the function should probably cache its result--you wouldn't want code that called it ten times to generate ten fresh dicts, would you?--and already we're most of the way to what I proposed in PEP 649.