Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python 3 isinstance

Reply
Thread Tools

Re: Python 3 isinstance

 
 
Terry Reedy
Guest
Posts: n/a
 
      01-15-2009
Lambert, David W (S&T) wrote:
> 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.


I agree. However, isinstance predates set. Hence the status quo.

 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      01-15-2009
On Jan 15, 3:35*am, Duncan Booth <(E-Mail Removed)> wrote:
> Terry Reedy <(E-Mail Removed)> wrote:
> > Lambert, David W (S&T) wrote:
> >> 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.

>
> > I agree. *However, isinstance predates set. *Hence the status quo.

>
> There is another reason which nobody has mentioned, and which might make it
> tricky to do a good implementation that does accept sets.
>
> The second argument to isinstance or issubclass is a classinfo where a
> classinfo is either a class or a tuple of classinfo. This makes it easy to
> group related types: you can just combine existing groups of types together
> without having to flatten tuples. The immutability of tuples guarantees
> that you cannot create a recursive structure this way.
>
> If you allow sets then the code which traverses the classinfo has to
> protect itself against the possibility of an infinite recursion.
>
> The same argument also applies to exception handlers which also accept a
> type or a tuple of things they accept.
>
> Of course you could allow either an existing classinfo or a set of types,
> but not allow the set to contain anything other than types. That would
> still prevent recursion but means you have to be stricter about knowing
> what you are combining. That probably wouldn't be a bad thing if everyone
> used sets consistently but I suspect would be messy if introduced at this
> stage.


I don't see what the big deal is. Right now isinstance accepts a type
or a tuple of types. The code could be changed to allow a type, or
any iterable the returns types (wherein every items of the sequence is
required to be a type). What's messy about that?


Carl Banks
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-15-2009
Duncan Booth wrote:
> Terry Reedy <(E-Mail Removed)> wrote:
>
>> Lambert, David W (S&T) wrote:
>>> 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.

>> I agree. However, isinstance predates set. Hence the status quo.
>>
>>

>
> There is another reason which nobody has mentioned, and which might make it
> tricky to do a good implementation that does accept sets.
>
> The second argument to isinstance or issubclass is a classinfo where a
> classinfo is either a class or a tuple of classinfo. This makes it easy to
> group related types: you can just combine existing groups of types together
> without having to flatten tuples. The immutability of tuples guarantees
> that you cannot create a recursive structure this way.
>
> If you allow sets then the code which traverses the classinfo has to
> protect itself against the possibility of an infinite recursion.
>
> The same argument also applies to exception handlers which also accept a
> type or a tuple of things they accept.
>
> Of course you could allow either an existing classinfo or a set of types,
> but not allow the set to contain anything other than types. That would
> still prevent recursion but means you have to be stricter about knowing
> what you are combining. That probably wouldn't be a bad thing if everyone
> used sets consistently but I suspect would be messy if introduced at this
> stage.


Nice explanation. A mathematical set is a frozen set of course (except
that mathematical sets can also be recursive! -- depending on the
particular set theory), and there is no literal syntax for that.

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-15-2009
On Jan 15, 1:08*pm, Duncan Booth <(E-Mail Removed)> wrote:
> Carl Banks <(E-Mail Removed)> wrote:
> > I don't see what the big deal is. *Right now isinstance accepts a type
> > or a tuple of types. *The code could be changed to allow a type, or
> > any iterable the returns types (wherein every items of the sequence is
> > required to be a type). *What's messy about that?

>
> No, it isn't restricted to a type or a tuple of types: right now isinstance
> accepts X where X is a type or a tuple of X. The definition is recursive,
> and the data structure can be deeply nested, but (because tuples are
> immutable) it cannot be infinite.
>
> e.g.>>> isinstance('x', (((((int, (float, str)),),),),))
>
> True


Oh. Not sure what the use case for THAT was, but that would make
things messy.


Carl Banks
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-16-2009
On Fri, 16 Jan 2009 09:58:53 +0000, Duncan Booth wrote:

> That still leaves the question whether anyone has ever actually taken
> advantage of this feature. You can do isinstance(x, (IntType, LongType,
> StringTypes)) but I didn't even know that StringTypes existed until a
> couple of moments ago, and I intend to forget about it again
> immediately. Grepping through the Python libraries on my system it looks
> like a lot of programmers don't even realise you can combine types into
> a tuple.



I do it a lot, except I don't bother with IntType, LongType etc (I assume
you imported them from the types module). I just do:

isinstance(x, (int, float)) # or whatever



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
isinstance(.., file) for Python 3 Ulrich Eckhardt Python 5 11-08-2012 03:14 PM
Python 3 isinstance Lambert, David W (S&T) Python 2 01-16-2009 02:01 AM
Re: Python 3 isinstance Lambert, David W (S&T) Python 2 01-15-2009 06:25 AM
Re: isInstance problem Ben Jessel Java 0 07-10-2003 10:26 AM
Re: isInstance problem Joona I Palaste Java 4 07-02-2003 05:48 AM



Advertisments