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


Groups > comp.lang.python > #95677 > unrolled thread

isinstance() and multiple inheritance/ctypes

Started byRob Gaddi <rgaddi@technologyhighland.invalid>
First post2015-08-26 21:21 +0000
Last post2015-08-28 13:25 +1000
Articles 9 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#95677 — isinstance() and multiple inheritance/ctypes

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-08-26 21:21 +0000
Subjectisinstance() and multiple inheritance/ctypes
Message-ID<mrlalj$nlt$1@dont-email.me>
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
-------------------------------------------------------

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.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [next] | [standalone]


#95678

FromTerry Reedy <tjreedy@udel.edu>
Date2015-08-26 17:46 -0400
Message-ID<mailman.66.1440625616.11709.python-list@python.org>
In reply to#95677
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

[toc] | [prev] | [next] | [standalone]


#95679

FromChris Angelico <rosuav@gmail.com>
Date2015-08-27 09:12 +1000
Message-ID<mailman.67.1440630784.11709.python-list@python.org>
In reply to#95677
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__

[toc] | [prev] | [next] | [standalone]


#95683

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-08-26 23:42 +0000
Message-ID<mrliu0$nlt$2@dont-email.me>
In reply to#95679
On Thu, 27 Aug 2015 09:12:55 +1000, Chris Angelico wrote:
> 
> 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
> 

Yep, you're quite right.  bf is a subclass, not an instance, hence 
issubclass is True and isinstance is False.  One of those days with too 
many lines of code and not enough cups of coffee to compensate.

Thanks for the help, my stupid mistake.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#95680

FromChris Angelico <rosuav@gmail.com>
Date2015-08-27 09:14 +1000
Message-ID<mailman.68.1440630846.11709.python-list@python.org>
In reply to#95677
On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico <rosuav@gmail.com> wrote:
> Or is there a magic __isinstance__

Argh, keyed the wrong thing and sent the post prematurely. Meant to say:

Or is there a magic __instancecheck__ method somewhere that I'm not aware of?

ChrisA

[toc] | [prev] | [next] | [standalone]


#95689

FromSteven D'Aprano <steve@pearwood.info>
Date2015-08-27 22:59 +1000
Message-ID<55df099e$0$1638$c3e8da3$5496439d@news.astraweb.com>
In reply to#95680
On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote:

> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico <rosuav@gmail.com> wrote:
>> Or is there a magic __isinstance__
> 
> Argh, keyed the wrong thing and sent the post prematurely. Meant to say:
> 
> Or is there a magic __instancecheck__ method somewhere that I'm not aware
> of?

Yes -- see Terry Reedy's post. 

isinstance(inst, klass) ends up calling 

type(klass).__instancecheck__(klass, inst)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#95693

FromChris Angelico <rosuav@gmail.com>
Date2015-08-28 00:33 +1000
Message-ID<mailman.75.1440686010.11709.python-list@python.org>
In reply to#95689
On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote:
>
>> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico <rosuav@gmail.com> wrote:
>>> Or is there a magic __isinstance__
>>
>> Argh, keyed the wrong thing and sent the post prematurely. Meant to say:
>>
>> Or is there a magic __instancecheck__ method somewhere that I'm not aware
>> of?
>
> Yes -- see Terry Reedy's post.
>
> isinstance(inst, klass) ends up calling
>
> type(klass).__instancecheck__(klass, inst)

I'm aware that it _can_ exist, but I was asking if one _did_ exist.

ChrisA

[toc] | [prev] | [next] | [standalone]


#95715

FromSteven D'Aprano <steve@pearwood.info>
Date2015-08-28 13:02 +1000
Message-ID<55dfcf4e$0$1663$c3e8da3$5496439d@news.astraweb.com>
In reply to#95693
On Fri, 28 Aug 2015 12:33 am, Chris Angelico wrote:

> On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano <steve@pearwood.info>
> wrote:
>> On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote:
>>
>>> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico <rosuav@gmail.com>
>>> wrote:
>>>> Or is there a magic __isinstance__
>>>
>>> Argh, keyed the wrong thing and sent the post prematurely. Meant to say:
>>>
>>> Or is there a magic __instancecheck__ method somewhere that I'm not
>>> aware of?
>>
>> Yes -- see Terry Reedy's post.
>>
>> isinstance(inst, klass) ends up calling
>>
>> type(klass).__instancecheck__(klass, inst)
> 
> I'm aware that it _can_ exist, but I was asking if one _did_ exist.


Er, I'm still not getting you. Of course one exists, otherwise isinstance
would end up raising an exception. 

py> type.__instancecheck__
<method '__instancecheck__' of 'type' objects>

What am I missing?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#95716

FromChris Angelico <rosuav@gmail.com>
Date2015-08-28 13:25 +1000
Message-ID<mailman.95.1440732351.11709.python-list@python.org>
In reply to#95715
On Fri, Aug 28, 2015 at 1:02 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Fri, 28 Aug 2015 12:33 am, Chris Angelico wrote:
>
>> On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano <steve@pearwood.info>
>> wrote:
>>> On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote:
>>>
>>>> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico <rosuav@gmail.com>
>>>> wrote:
>>>>> Or is there a magic __isinstance__
>>>>
>>>> Argh, keyed the wrong thing and sent the post prematurely. Meant to say:
>>>>
>>>> Or is there a magic __instancecheck__ method somewhere that I'm not
>>>> aware of?
>>>
>>> Yes -- see Terry Reedy's post.
>>>
>>> isinstance(inst, klass) ends up calling
>>>
>>> type(klass).__instancecheck__(klass, inst)
>>
>> I'm aware that it _can_ exist, but I was asking if one _did_ exist.
>
>
> Er, I'm still not getting you. Of course one exists, otherwise isinstance
> would end up raising an exception.
>
> py> type.__instancecheck__
> <method '__instancecheck__' of 'type' objects>
>
> What am I missing?

Hi, I'm trying to figure out why my object is coming out as an
instance of list, when it shouldn't be.

1) Is it really the object you think it is?
2) Does that type of object define __instancecheck__?

There are certain normal, default expectations. We all know what those
are. I was asking if these expectations had been modified in any way.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web