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


Groups > comp.lang.python > #34078

Re: weird isinstance/issubclass behavior?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: weird isinstance/issubclass behavior?
Date 2012-11-29 16:00 -0500
References <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.370.1354222886.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 11/29/2012 9:59 AM, lars van gemerden wrote:
> Hi,
>
> I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file.
>
> Basically if i import a class as:
>
>      from mod1.mod2 import A
> or:
>      from mod0.mod1.mod2 import A
>
> which both result in importing the same class,

As other said, both import the same abstract class but create two 
different concrete class objects.

 > a call to isinstance(inst, A) in another module can have a different 
output.
> In this module
>      print type(inst), A, isinstance(inst, A), issubclass(type(inst), A)
> gives:
>      <class 'mod0.mod1.mod2.A'> <class 'mod1.mod2.A'> False False

Add print id(type(inst)), id(A) and you will see that they are different 
objects. The isinstance and issubclass calls compare the classes by 
identity (the default meaning of ==) and so False, False are correct.

-- 
Terry Jan Reedy

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


Thread

weird isinstance/issubclass behavior? lars van gemerden <lars@rational-it.com> - 2012-11-29 06:59 -0800
  Re: weird isinstance/issubclass behavior? Chris Angelico <rosuav@gmail.com> - 2012-11-30 02:08 +1100
  Re: weird isinstance/issubclass behavior? lars van gemerden <lars@rational-it.com> - 2012-11-29 08:07 -0800
  Re: weird isinstance/issubclass behavior? Terry Reedy <tjreedy@udel.edu> - 2012-11-29 16:00 -0500

csiph-web