[Python-bugs-list] [ python-Bugs-467267 ] isinstance depends on import form
noreply@sourceforge.net
noreply@sourceforge.net
Tue, 02 Oct 2001 11:56:25 -0700
Bugs item #467267, was opened at 2001-10-02 11:56
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470
Category: Python Interpreter Core
Group: Python 2.1.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Luis P Caamano (lcaamano)
Assigned to: Nobody/Anonymous (nobody)
Summary: isinstance depends on import form
Initial Comment:
isinstance(obj, cls) fails if cls is in a package and
the import line of the obj creator is different from
the import line in the code calling isinstance.
It seems to me that the problem is the address test in
PyObject_IsInstance(), because different import forms
produce objects with different addresses.
A limited workaround would be to test for class name
but that doesn't include subclasses or types. So,
instead of:
if isinstance(obj, class):
use
if obj.__class__.__name__ == 'classname':
-------Sample--------------------
Python 2.1.1 (#2, Jul 25 2001, 17:54:11)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on
linux2
Type "copyright", "credits" or "license" for more
information.
>>> from spam.eggs import SpamAndEggs
>>> import spam.eggs
>>> '0x%x' % id(spam.eggs.SpamAndEggs)
'0x8161ce4'
>>> '0x%x' % id(SpamAndEggs)
'0x814c29c'
>>> sag = spam.eggs.SpamAndEggs()
>>> isinstance(sag, SpamAndEggs)
0
>>> isinstance(sag, spam.eggs.SpamAndEggs)
1
>>> sag.__class__
<class spam.eggs.SpamAndEggs at 0x8161ce4>
>>> sag.__class__.__name__
'SpamAndEggs'
>>> SpamAndEggs.__name__
'SpamAndEggs'
>>> spam.eggs.SpamAndEggs.__name__
'SpamAndEggs'
>>>
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470