[Python-ideas] Delay evaluation of annotations

Neil Girdhar mistersheik at gmail.com
Tue Sep 27 08:46:30 EDT 2016


Yes, I understand that, but I don't see how that would help at all with
annotations.  Aren't annotations also evaluated at "compile time"?

On Tue, Sep 27, 2016 at 8:14 AM Oleg Broytman <phd at phdru.name> wrote:

> On Tue, Sep 27, 2016 at 11:54:40AM +0000, Neil Girdhar <
> mistersheik at gmail.com> wrote:
> > I don't understand why that would work and this clearly doesn't?
> >
> > Mutual2 = "Mutual2" # Pre-declare Mutual2
> >
> > class Mutual1:
> >     def spam(self, x=Mutual2):
>                        ^^^^^^^ - calculated at compile time,
>                                  not at run time
> >         print(type(x))
> >
> > class Mutual2:
> >     def spam(self):
> >         pass
> >
> > Mutual1().spam()
> >
> > prints class "str" rather than "type".
>
>    Try this:
>
> class Mutual1:
>     def spam(self, x=None):
>         if x is None:
>             x = Mutual2
>         print(type(x))
>
> class Mutual2:
>     def spam(self):
>         pass
>
> Mutual1().spam()
>
> Oleg.
> --
>      Oleg Broytman            http://phdru.name/            phd at phdru.name
>            Programmers don't die, they just GOSUB without RETURN.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160927/d84f3104/attachment.html>


More information about the Python-ideas mailing list