[Python-ideas] Delay evaluation of annotations
Neil Girdhar
mistersheik at gmail.com
Tue Sep 27 07:54:40 EDT 2016
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):
print(type(x))
class Mutual2:
def spam(self):
pass
Mutual1().spam()
prints class "str" rather than "type".
On Tue, Sep 27, 2016 at 6:20 AM Stephen J. Turnbull <
turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:
> Neil Girdhar writes:
>
> > 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?
>
> Why not just expose it through a simple assignment?
>
> https://mail.python.org/pipermail/python-ideas/2016-September/042563.html
>
> Note that this also works for typechecking in PEP 484 checkers that
> allow forward reference via the stringified class name. See also
> https://mail.python.org/pipermail/python-ideas/2016-September/042544.html,
> which would allow eliding the assignment, but pollutes the class namespace.
>
> "Simple is better than complex."
>
> This feature is still looking for a persuasive use that needs it, and
> not something simpler.
>
> Steve
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160927/32b9f560/attachment.html>
More information about the Python-ideas
mailing list