Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(python': 0.05; 'method.': 0.05; 'ugly': 0.07; '-------': 0.09; 'derived': 0.09; 'methods,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'subject:ctypes': 0.09; 'jan': 0.11; '(),': 0.16; 'customizing': 0.16; 'linux)': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'stuff.': 0.16; 'wrote:': 0.16; 'why.': 0.18; '(on': 0.22; 'ctypes': 0.22; 'trying': 0.22; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'figure': 0.27; 'idea': 0.28; 'structure,': 0.29; '-----': 0.29; 'array': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'classes': 0.30; 'checks': 0.30; 'skip:_ 10': 0.32; 'running': 0.34; 'skip:- 50': 0.35; 'false': 0.35; 'instance': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'received:org': 0.37; 'things': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'more': 0.63; '3.4': 0.84; 'indirect': 0.84; 'lying': 0.84; 'manual,': 0.84; 'union,': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: isinstance() and multiple inheritance/ctypes Date: Wed, 26 Aug 2015 17:46:17 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440625616 news.xs4all.nl 23752 [2001:888:2000:d::a6]:37310 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95678 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