Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95678

Re: isinstance() and multiple inheritance/ctypes

From Terry Reedy <tjreedy@udel.edu>
Subject Re: isinstance() and multiple inheritance/ctypes
Date 2015-08-26 17:46 -0400
References <mrlalj$nlt$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.66.1440625616.11709.python-list@python.org> (permalink)

Show all headers | View raw


On 8/26/2015 5:21 PM, Rob Gaddi wrote:
> I'm running into some strangeness trying to work with the bitfield module
> from my ctypes-bitfield package (on PyPi).  I'm trying to use isinstance
> (), and it's kinda sorta lying to me.

isinstace(inst, klass) is implemented as
klass.__instancecheck__(inst) ==
type(klass).__instancecheck__(klass, inst)
In other words, __instancecheck__ is a metaclass method.
Reference Manual, 3.3.4. Customizing instance and subclass checks

> ----- IPython session (Python 3.4 under Linux) -------
> In [649]: bf.__mro__
> Out[649]: (bitfield._TD, _ctypes.Union, _ctypes._CData,
> bitfield.Bitfield, builtins.object)

Find the metaclasses with "for cl in bf.__mro__: print(type(bf))" and 
then take a look at the __instancecheck__ method if not 'type'.

> In [650]: isinstance(bf, bitfield.Bitfield)
> Out[650]: False
>
> In [651]: bf.__bases__
> Out[651]: (_ctypes.Union, bitfield.Bitfield)
>
> In [652]: bf.__bases__[1]
> Out[652]: bitfield.Bitfield
>
> In [653]: bf.__bases__[1] is bitfield.Bitfield
> Out[653]: True
> -------------------------------------------------------
>
> Is there an issue with isinstance and multiple inheritance?  Conversely
> is there an issue with isinstance and ctypes derived classes (Bitfield
> isn't, but Bitfield is a mixin that always works with Unions)  I know
> that ctypes classes can get really wonky on this stuff.
>
> More generally, is there any good way to introspect ctypes derived
> classes?  I have to figure out whether things are derived from Structure,
> Union, Array etc. through some ugly indirect methods, and have no idea
> why.
>


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

isinstance() and multiple inheritance/ctypes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-08-26 21:21 +0000
  Re: isinstance() and multiple inheritance/ctypes Terry Reedy <tjreedy@udel.edu> - 2015-08-26 17:46 -0400
  Re: isinstance() and multiple inheritance/ctypes Chris Angelico <rosuav@gmail.com> - 2015-08-27 09:12 +1000
    Re: isinstance() and multiple inheritance/ctypes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-08-26 23:42 +0000
  Re: isinstance() and multiple inheritance/ctypes Chris Angelico <rosuav@gmail.com> - 2015-08-27 09:14 +1000
    Re: isinstance() and multiple inheritance/ctypes Steven D'Aprano <steve@pearwood.info> - 2015-08-27 22:59 +1000
      Re: isinstance() and multiple inheritance/ctypes Chris Angelico <rosuav@gmail.com> - 2015-08-28 00:33 +1000
        Re: isinstance() and multiple inheritance/ctypes Steven D'Aprano <steve@pearwood.info> - 2015-08-28 13:02 +1000
          Re: isinstance() and multiple inheritance/ctypes Chris Angelico <rosuav@gmail.com> - 2015-08-28 13:25 +1000

csiph-web