Velocity Reviews > Re: Python 3 isinstance

# Re: Python 3 isinstance

Lambert, David W (S&T)
Guest
Posts: n/a

 01-15-2009
Overly terse. I do mean that this is illegal:

isinstance(s, {str, bytes})

tuples have order, immutability, and the possibility of repeat items.

A set is most reasonable in a mathematical sense.

John Machin
Guest
Posts: n/a

 01-15-2009
On Jan 15, 2:03 pm, "Lambert, David W (S&T)" <(E-Mail Removed)>
wrote:
> Overly terse. I do mean that this is illegal:
>
> isinstance(s, {str, bytes})
>
> tuples have order, immutability, and the possibility of repeat items.

In the anticipated/usual use case (the type/class names are hard-
coded):
* order is a very mild plus (you can list them in descending order of
(imagined) probability)
* immutability is not a problem
* the number of items is not likely to be so large that duplicate
entries would creep in

> A set is most reasonable in a mathematical sense.

This is true -- distinguishing carefully between "most reasonable" and
"the most reasonable"

Do you actually have a use case for dynamically assembling a
collection of classes/types for use with isinstance(), or are you
propelled solely by mathematical reasonableness?

Cheers,
John

Steven D'Aprano
Guest
Posts: n/a

 01-15-2009
On Wed, 14 Jan 2009 22:03:37 -0500, Lambert, David W (S&T) wrote:

> Overly terse. I do mean that this is illegal:
>
> isinstance(s, {str, bytes})

*shrug*

Just change the {} to () and it will work fine.

Or do this:

isinstance(s, tuple({str, bytes}))

> tuples have order, immutability, and the possibility of repeat items.

Yes. And why is this a problem?

> A set is most reasonable in a mathematical sense.

What is the benefit of using sets instead of tuples inside isinstance?

--
Steven