Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95679
| References | <mrlalj$nlt$1@dont-email.me> |
|---|---|
| Date | 2015-08-27 09:12 +1000 |
| Subject | Re: isinstance() and multiple inheritance/ctypes |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.67.1440630784.11709.python-list@python.org> (permalink) |
On Thu, Aug 27, 2015 at 7:21 AM, Rob Gaddi
<rgaddi@technologyhighland.invalid> 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.
>
> ----- IPython session (Python 3.4 under Linux) -------
> In [649]: bf.__mro__
> Out[649]: (bitfield._TD, _ctypes.Union, _ctypes._CData,
> bitfield.Bitfield, builtins.object)
>
> 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
> -------------------------------------------------------
You're omitting some context here, but after poking around with your
module a bit, I'm guessing you created bf somewhat thus?
>>> bf = bitfield.make_bf("bf", (("asdf",bitfield.c_uint,2),))
>>> bf
<class 'bitfield.bf'>
>>> bf.__mro__
(<class 'bitfield.bf'>, <class '_ctypes.Union'>, <class
'_ctypes._CData'>, <class 'bitfield.Bitfield'>, <class 'object'>)
>>> type(bf)
<class '_ctypes.UnionType'>
>>> type(bf).__mro__
(<class '_ctypes.UnionType'>, <class 'type'>, <class 'object'>)
If that's the case, then it's quite correct: bf is not a Bitfield, it
is a subclass of bitfield.
>>> isinstance(bf,type)
True
>>> isinstance(bf(),bitfield.Bitfield)
True
>>> issubclass(bf,bitfield.Bitfield)
True
Or is there a magic __isinstance__
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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