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


Groups > comp.lang.python > #34068

Re: weird isinstance/issubclass behavior?

References <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com>
Date 2012-11-30 02:08 +1100
Subject Re: weird isinstance/issubclass behavior?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.365.1354201710.29569.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Nov 30, 2012 at 1:59 AM, lars van gemerden <lars@rational-it.com> wrote:
> 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.

What you may be seeing there is that you've imported the module twice.
There are two entirely separate module objects, taken from the same
file. Something instantiated from one class isn't an instance of the
other class even if they're indistinguishable classes; a little
monkeypatching might show you what's going on (fiddle with one class,
check the other, fiddle hasn't happened).

As a general rule, importing a module in different ways is considered
dangerous. Be consistent, and then this sort of oddity won't occur -
and you also won't have other oddities, eg with other global state.

ChrisA

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