Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importing': 0.04; 'class,': 0.07; 'correct.': 0.07; 'false,': 0.07; 'objects.': 0.09; 'output,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; '(the': 0.15; 'a),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'jan': 0.18; 'module': 0.19; 'file.': 0.20; 'import': 0.21; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'header:X -Complaints-To:1': 0.28; 'subject:/': 0.28; 'van': 0.29; 'class': 0.29; 'classes': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'another': 0.33; 'false': 0.35; 'identity': 0.35; 'path': 0.35; 'said,': 0.35; 'subject:?': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'compare': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'strange': 0.62; 'different': 0.63; 'behavior': 0.64; 'as:': 0.75; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: weird isinstance/issubclass behavior? Date: Thu, 29 Nov 2012 16:00:48 -0500 References: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <1cad82ec-8241-486c-9c8b-c7d6ca427adc@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354222886 news.xs4all.nl 6913 [2001:888:2000:d::a6]:46736 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34078 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: > 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