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


Groups > comp.lang.python > #34070

Re: weird isinstance/issubclass behavior?

Newsgroups comp.lang.python
Date 2012-11-29 08:07 -0800
References <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com>
Message-ID <e54debe4-c320-431a-93c8-d2f2b9e4d5dc@googlegroups.com> (permalink)
Subject Re: weird isinstance/issubclass behavior?
From lars van gemerden <lars@rational-it.com>

Show all headers | View raw


On Thursday, November 29, 2012 3:59:37 PM UTC+1, 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, 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
> 
> 
> 
> resp.
> 
> 
> 
>     <class 'mod0.mod1.mod2.A'> <class 'mod0.mod1.mod2.A'> True True
> 
> 
> 
> which seems somewhat logical, but in my case a strange gotcha. My question is:
> 
> Is this intended, inevitable or a bug?
> 
> 
> 
> Cheers, Lars
> 
> 
> 
> PS: this is somewhat simpler than the actual case i've encountered, and i haven't tested this exact case, but for now i hope this is enough to get some of your insight.

I know for sure that the imports both import the same file, though if i understand you correctly, it creates 2 different module objects? Are module object only created on an import statement?  

The 2 parameters of isinstance() follow a very different import path. 

Anyway, to not spend too much time us this, i'll just file it under 'dangerous' and use the second import statement. It does solve my problem. 

Thanks, Lars

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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