Python 3 isinstance

Discussion in 'Python' started by Lambert, David W (S&T), Jan 14, 2009.

  1. Please, why isn't a set permitted as the second argument to isinstance?
    Lambert, David W (S&T), Jan 14, 2009
    #1
    1. Advertising

  2. Lambert, David W (S&T)

    Carl Banks Guest

    On Jan 14, 4:14 pm, "Lambert, David W (S&T)" <>
    wrote:
    > Please, why isn't a set permitted as the second argument to isinstance?


    The Python development team might be open to adding it if you were to
    submit a patch. The use case is fairly obvious. I doubt any of them
    would respond to a wishlist request, though.

    As a workaround, you can just cast the set to a tuple like this:

    isinstance(x,tuple(s))


    Carl Banks
    Carl Banks, Jan 14, 2009
    #2
    1. Advertising

  3. On Jan 14, 3:14 pm, "Lambert, David W (S&T)" <>
    wrote:
    > Please, why isn't a set permitted as the second argument to isinstance?


    The real problem is it would be misleading. isinstance(a, myset)
    suggests implementation like type(a) in myset, but it's really any
    (isinstance(a, t) for t in myset).

    If myset is arbitrarily large (as the examples suggest), and you're
    checking on every call, the code would be so slow as to be considered
    broken. This invalidates the use case.

    The abc module in 2.6/3.0 is a possible alternative. Create an ABC
    type, register each type in myset as part of it, then do isinstance(a,
    MyABC). Internally the ABC will cache lookups, effectively getting
    you back to O(1) cost.
    Rhamphoryncus, Jan 16, 2009
    #3
    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,025
    George W. Cherry
    Jul 2, 2003
  2. Ben Jessel

    Re: isInstance problem

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

    Re: Python 3 isinstance

    Lambert, David W (S&T), Jan 15, 2009, in forum: Python
    Replies:
    2
    Views:
    262
    Steven D'Aprano
    Jan 15, 2009
  4. Terry Reedy

    Re: Python 3 isinstance

    Terry Reedy, Jan 15, 2009, in forum: Python
    Replies:
    4
    Views:
    1,522
    Steven D'Aprano
    Jan 16, 2009
  5. Ulrich Eckhardt

    isinstance(.., file) for Python 3

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

Share This Page