[Python-ideas] Delay evaluation of annotations

Neil Girdhar mistersheik at gmail.com
Tue Sep 27 03:29:00 EDT 2016



On Friday, September 23, 2016 at 2:23:58 AM UTC-4, Nick Coghlan wrote:
>
> On 23 September 2016 at 15:50, Greg Ewing <greg.... at canterbury.ac.nz 
> <javascript:>> wrote:
> > אלעזר wrote:
> >>
> >>     it feels like a
> >>     placeholder for this meaning would be better.  E.g.:
> >>
> >>     class A:
> >>         def __add__(self, other: CLS) -> CLS: ...
> >
> >
> > That's fine for a class that refers to itself, but
> > what about classes that refer to each other? This
> > only addresses a small part of the problem.
>
> Same answer as with any other circular dependency: the code smell is
> the circular dependency itself, not the awkwardness of the syntax for
> spelling it. If the string based "circular reference here!" spelling
> really bothers you, refactor to eliminate the circularity (e.g. by
> extracting a base class or an implementation independent interface
> definition), rather than advocating to make the spelling less
> obnoxious.
>
> The difference between that and the "methods referring to the class
> they're defined in" case is that it's likely to be pretty normal to
> want to do the latter, so it may prove worthwhile to provide a cleaner
> standard spelling for it. The counter-argument is the general
> circularity one above: do you *really* need instances of the
> particular class being defined? Or is there a more permissive
> interface based type signature you could specify instead? Or perhaps
> no type signature at all, and let ducktyping sort it out implicitly at
> runtime?
>

I agree that circularity should in general be avoided, but it's not always 
possible or elegant to do that.  Sometimes you really need two classes to 
refer to each other.  In that case, why not expose your placeholder idea to 
the user via a library?  You have one function that generates placeholder 
singletons (generate_placeholder()), and another function to walks a class 
object and replaces a placeholder with a given value 
(replace_placeholder(placeholder, cls, value)).

Best,

Neil
 

> Cheers,
> Nick.
>
> -- 
> Nick Coghlan   |   ncog... at gmail.com <javascript:>   |   Brisbane, 
> Australia
> _______________________________________________
> Python-ideas mailing list
> Python... at python.org <javascript:>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160927/429426a6/attachment.html>


More information about the Python-ideas mailing list