Re: Python 3 isinstance

Discussion in 'Python' started by Terry Reedy, Jan 15, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    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.
     
    Terry Reedy, Jan 15, 2009
    #1
    1. Advertising

  2. Terry Reedy

    Carl Banks Guest

    On Jan 15, 3:35 am, Duncan Booth <> wrote:
    > Terry Reedy <> 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
     
    Carl Banks, Jan 15, 2009
    #2
    1. Advertising

  3. Terry Reedy

    Terry Reedy Guest

    Duncan Booth wrote:
    > Terry Reedy <> 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.
     
    Terry Reedy, Jan 15, 2009
    #3
  4. Terry Reedy

    Carl Banks Guest

    On Jan 15, 1:08 pm, Duncan Booth <> wrote:
    > Carl Banks <> 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
     
    Carl Banks, Jan 15, 2009
    #4
  5. 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
     
    Steven D'Aprano, Jan 16, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste

    Re: isInstance problem

    Joona I Palaste, Jul 1, 2003, in forum: Java
    Replies:
    4
    Views:
    1,030
    George W. Cherry
    Jul 2, 2003
  2. Ben Jessel

    Re: isInstance problem

    Ben Jessel, Jul 10, 2003, in forum: Java
    Replies:
    0
    Views:
    396
    Ben Jessel
    Jul 10, 2003
  3. Lambert, David W (S&T)

    Python 3 isinstance

    Lambert, David W (S&T), Jan 14, 2009, in forum: Python
    Replies:
    2
    Views:
    419
    Rhamphoryncus
    Jan 16, 2009
  4. Lambert, David W (S&T)

    Re: Python 3 isinstance

    Lambert, David W (S&T), Jan 15, 2009, in forum: Python
    Replies:
    2
    Views:
    265
    Steven D'Aprano
    Jan 15, 2009
  5. Ulrich Eckhardt

    isinstance(.., file) for Python 3

    Ulrich Eckhardt, Nov 8, 2012, in forum: Python
    Replies:
    5
    Views:
    493
    Stefan Behnel
    Nov 8, 2012
Loading...

Share This Page