May be, with isinstance(object:Any,classinfo: Tuple | Union) ?
it's possible to accept isinstance(anUnion, [Union]) # Force tuple for Union isinstance(aTuple, [Tuple] # Force Tuple isinstance(aStr, int | str) Correct ? Le ven. 30 août 2019 à 19:29, Andrew Barnert <[email protected]> a écrit : > On Aug 30, 2019, at 02:09, Philippe Prados <[email protected]> > wrote: > > Why not extend isInstance to : > > isinstance(object:Any,classinfo: Iterable | Union) ? > > > How would you pass a single type? > > And once you allow a single type, you shouldn’t have to do anything > special to allow a Union, because a Union is already a type. > > And there’s a good reason isinstance > only takes tuples of types, not arbitrary iterables: because a type can be > iterable (e.g., an Enum contains all of its members), so it would make > single types ambiguous. Just like str.endswith can’t take a string or > iterable (strings contain their characters), so it gets the same solution > used there and dozens of other places: tuples are special-cased. > > If we already have | (you’re using it in your annotation), we don’t have a > problem to solve in the first place. The problem only comes up if we allow > sets of types as shorthand for unions in annotations instead. > > And the only problem is that sets as shorthand don’t match the “tuples > are special” rule in isinstance and elsewhere, so that rule has to change > to something like “tuples and sets (and frozensets?) are special”. I don’t > think we need (or want) any more complicated change than that. > >
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/AZRPCLETJFNDTBE56YGOUCNYKFVRAQRE/ Code of Conduct: http://python.org/psf/codeofconduct/
