On Thu, Sep 5, 2019 at 1:02 AM Andrew Barnert <abarn...@yahoo.com> wrote:
>
> > I dislike runtime behavior of static types because I am very afraid 
> > accidental
> > large performance or memory footprint regression.
> >
> > ABC has extension module for speedup, but `isinstance([], Iterable)` is
> > 4x slower than `isinstance([], (str, list)`.
>
> Does the ABC use the extension module to speed up isinstance checks? 
> (Couldn’t you just repeat your test with typing.Iterable—which can be tested; 
> it’s only instantiated types like Iterable[int] that can’t, not the generics 
> themselves—and see if it’s significantly slower than collections.abc.Iterable 
> instead of guessing?)
>

Yes. See this code:
https://github.com/python/cpython/blob/b9a0376b0dedf16a2f82fa43d851119d1f7a2707/Modules/_abc.c#L477-L502

ABC caches the instance check.  So Iterable.__subclasshook__ is called
only once in my benchmark.
If __subclasshook__ is called every time when isinstance is called, it
will be much slower.


>  And you’re right, because int|str _looks_ better than (int, str) here, many 
> people will be encouraged to use it even though it’s slower, which could 
> potentially be a bad thing for some programs.

That's exactly my point.  If we say "you can use `isinstance(x, int |
str)` for now", people
may think it is a new and recommended way to write it.

I prefer "there is one preferable way to do it" to "there is new way
to do it but it may be much
slower and use much memory, so you shouldn't use it unless you can
ignore performance."

Regards,
-- 
Inada Naoki  <songofaca...@gmail.com>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3PJKNQ64J3XCEJUC5G26X4TTJH7WXRM6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to