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


Groups > comp.lang.python > #4808

Re: seems like a bug in isinstance()

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
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; 'bug': 0.02; 'subject:bug': 0.04; 'instance': 0.05; 'suppose': 0.05; 'definitions': 0.07; 'imports': 0.07; 'python': 0.07; 'func': 0.09; 'received:209.85.160.174': 0.09; 'received:mail- gy0-f174.google.com': 0.09; 'subject:()': 0.09; 'output': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'earlier,': 0.16; 'long,': 0.16; 'subsequent': 0.16; '(i.e.': 0.18; 'importing': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'seems': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; '(and': 0.22; 'referring': 0.23; 'thus': 0.24; 'point,': 0.25; 'chris': 0.27; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.160': 0.28; 'fri,': 0.29; 'class': 0.29; 'class.': 0.29; 'fine.': 0.29; 'instead.': 0.29; "python's": 0.29; 'cc:addr:python.org': 0.31; 'situation,': 0.31; 'whereas': 0.31; 'separate': 0.31; 'all,': 0.31; 'import': 0.32; "i've": 0.33; 'sufficient': 0.33; 'someone': 0.33; 'created': 0.33; 'module': 0.33; "isn't": 0.34; 'reference': 0.34; 'skip:" 10': 0.34; 'file': 0.35; 'point': 0.35; 'instances': 0.35; 'too': 0.36; 'rather': 0.36; 'think': 0.36; 'skip:o 20': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'ways': 0.38; 'so,': 0.38; 'received:209': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'twice': 0.60; '2011': 0.62; 'ever': 0.65; 'cause': 0.65; 'due': 0.67; 'subject:like': 0.68; 'dealing': 0.71; 'encountered': 0.73; 'smarter': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=SgxWl1OS+Ze6aST/ppQPk7iz9PADXNCoBweHW4phwkA=; b=eqVNCHqgzcXcofLWjxtkZb6H+/7GUnu7jSFGfHkdO3D+8x+asIQVC18ige4xONtg+1 WT8EzjcoEKXqa+Ev5JuzjeZSkcc2vLVqwanep0pXwuoFangV/2X3iXop86f09lK0pwyN Ls7w1v++0CnwBLoK0js7Jk4znzmNZidWjccDU=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=RxnoMBDx5cyOZUykH+IY5plRbGG3RCK+Tk6WqRGE0XITxGiXUkpipk01rYYhFiW3Wf VGFpkwYE6jfvvSq9JkXY54xBOeU0mTPs5zFmgZuoHEkC5DPk0L+Z5O17xOOO+1N7F9U0 /F3M9L+XOP6Fiy6u6kKHhXFGWua6EcYT9TTSk=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <15501b92-9392-45d4-a337-c4064a237813@w36g2000vbi.googlegroups.com>
References <15501b92-9392-45d4-a337-c4064a237813@w36g2000vbi.googlegroups.com>
Date Fri, 6 May 2011 02:57:39 -0700
X-Google-Sender-Auth 6Drjk1Lmoq8IiAGcK__xWgZii6g
Subject Re: seems like a bug in isinstance()
From Chris Rebert <clp2@rebertia.com>
To dmitrey <dmitrey15@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1231.1304675862.9059.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 82.94.164.166
X-Trace 1304675862 news.xs4all.nl 32470 [::ffff:82.94.164.166]:54344
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4808

Show key headers only | View raw


On Fri, May 6, 2011 at 2:24 AM, dmitrey <dmitrey15@gmail.com> wrote:
> hi all,
>
> suppose I've created a class Point in file .../openopt/kernel/Point.py
>
> Consider the code in file .../somewhere/file1.py
> from openopt.kernel.Point import Point
> p = Point()
>
> now let's pass p into a func from .../openopt/kernel/file2.py and
> check
> from Point import Point
> isinstance(p, Point)
>
> So, it returns False!
>
> p is <Point.Point instance at 0x30801b8>, while Point is <class
> openopt.kernel.Point.Point at 0x2048e20>
>
> [Subject: seems like a bug in isinstance()]

This is due to a peculiarity of how (C)Python's import machinery
works; isinstance() is working just fine.
(And if you ever think you've found a bug in Python's built-ins, odds
are you haven't. Python has been around too long, someone ought to
have encountered it earlier, statistically speaking.)

Note how the class is referred to as both Point.Point and
openopt.kernel.Point.Point. This is because you did `from Point import
...` in file2.py, whereas in file1.py you did `from
openopt.kernel.Point import ...`. These 2 different ways of referring
to the same module are sufficient to "trick"/"outsmart" (C)Python and
cause it to import the same module twice as 2 separate instances (i.e.
it gets re-executed). Why the import machinery isn't smarter in this
situation, I don't recall.

The output of this should be illuminating:
print(Point, type(p), type(p) is Point, id(Point), id(type(p)))
As this demonstrates, you're dealing with 2 separate definitions of
the same Point class.

Solution: Avoid the implicitly-relative `from Point import ...` style
of import; always use the absolute `from openopt.kernel.Point import
...` style instead. Subsequent imports will thus reference the
already-previously-imported instance of a module rather than importing
a copy of it from scratch again.

Cheers,
Chris
--
http://rebertia.com

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


Thread

seems like a bug in isinstance() dmitrey <dmitrey15@gmail.com> - 2011-05-06 02:24 -0700
  Re: seems like a bug in isinstance() Chris Rebert <clp2@rebertia.com> - 2011-05-06 02:57 -0700
    Re: seems like a bug in isinstance() dmitrey <dmitrey15@gmail.com> - 2011-05-06 03:20 -0700
      Re: seems like a bug in isinstance() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 11:27 -0600
    Re: seems like a bug in isinstance() Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-07 20:53 +1200
      Re: seems like a bug in isinstance() dmitrey <dmitrey15@gmail.com> - 2011-05-07 03:04 -0700
        Re: seems like a bug in isinstance() Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-08 10:58 +1200

csiph-web