On Wed, Sep 4, 2019 at 1:15 PM Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
On Sep 3, 2019, at 19:45, Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Aug 29, 2019 at 06:20:55PM +0100, Rob Cliffe via Python-ideas wrote:
isinstance(x, str | int) ==> "is x an instance of str or int"
Er, is that necessary when you can already write isinstance(x, (str, int))
It's not *necessary* it's just nicer.
Definitely. It reads even more like what it means than the existing spelling, and it’s something novices are almost certain to expect to work, and so on.
But that implies that you can also write this:
isinstance(x, Union[str, int])
… because, after all, str|int is defined as meaning exactly that. Which implies that the current rule that instantiated genetic types cannot be used for runtime type checks needs to be changed.
That’s certainly plausible, and maybe reasonable. But as I understand it, the idea that there things can’t be used for runtime type checks was a fundamental guiding principle to the typing design. So, it’s not something to be changed lightly. Someone has to go back to the reason for that principle (which may not be clearly stated anywhere, in which case it has to be extracted from things that _have_ been argued), and then make the case for why it should be violated here. And I haven’t seen anyone make that case. (If someone has and I missed it, apologies; chunks of this thread keep getting flagged as spam for some reason…)
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)`. ``` $ python3 -m pyperf timeit -s 'from collections.abc import Iterable; x=[]' -- 'isinstance(x, Iterable)' ..................... Mean +- std dev: 280 ns +- 8 ns $ python3 -m pyperf timeit -s 'x=[]; T=(str, list)' -- 'isinstance(x, T)' ..................... Mean +- std dev: 73.1 ns +- 1.0 ns ``` Typing module doesn't have speedup extension. I expect`isinstance([], Union[str, list])` will be much slower than `isinstance([], (str, list))`. Regards, -- Inada Naoki <songofacandy@gmail.com>