New Features¶ PEP 563: Postponed Evaluation of Annotations (JNNC Technologies)

                                                  http://jnnctechnologies.com/
                               https://clanguagetraininginvizagjnnc.blogspot.com/  
                               https://javatrainingjnnctechnologies.blogspot.com/
                                  https://pythontraininginjnnctechnologies.blogspot.com/
                                      https://dotnetjnnctechnologies.blogspot.com/
                                                   https://softwarecoursesselenium.blogspot.com/
                                     https://summerinternshipinjnnctechnologies.blogspot.com/
                                           


The advent of type hints in Python uncovered two glaring usability issues with the functionality of annotations added in PEP 3107 and refined further in PEP 526:
  • annotations could only use names which were already available in the current scope, in other words they didn’t support forward references of any kind; and
  • annotating source code had adverse effects on startup time of Python programs.
Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime usingtyping.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.
Usability-wise, annotations now support forward references, making the following syntax valid:

class C:
    @classmethod
    def from_string(cls, source: str) -> C:
        ...

    def validate_b(self, obj: B) -> bool:
        ...

class B:
    ...
Since this change breaks compatibility, the new behavior needs to be enabled on a per-module basis in Python 3.7 using a __future__ import:
from __future__ import annotations
It will become the default in Python 4.0.
See also
PEP 563 – Postponed evaluation of annotations
PEP written and implemented by Łukasz Langa.

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();