[Python-Dev] Re: PEP: Deferred Evaluation Of Annotations Using Descriptors

2021年1月11日 19:20:12 -0800

On 1/11/21 6:34 PM, Paul Bryan wrote:
a.__annotations__ = o
assert a.__annotations__ == o
Would that assert fail? It depends on what type(o) is, which is surprising.
Equally surprising?:
a.__co_annotations__ = o
a.__annotations__
assert a.__co_annotations__ == o
Well, since you asked, no. It's a good point, but I think my example is a touch more "surprising". In my example, a.__annotations__ has two different True values; in yours, a.__co_annotations__ goes from True to False. It's acting more like a cache. Also consider: if you set o.__annotations__ to a function, what if you want to examine the function later? What if you want to examine the function built by Python?
 def enhance_annotations(co_annotations):
   def enhance():
     d = co_annotations()
     d['extra'] = ...
     return d
 o.__co_annotations__ = enhance_annotations(o.__co_annotations__)
Finally, it's just a code smell to have one attribute support such a bewildering variety of types. Of course, this /works,/ but it's bad hygiene to store such violently different types in what is ostensibly the same attribute.
Cheers,
//arry/
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/5OSZHRZUXD2DZG54ROUYQ3IQW42DFUAK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to