According to Andrew Kuchling's and Moshe Zadka's "Python 2.0 what's new
list", isinstance() and issubclass() already works on
ExtensionClass-based stuff in Python 1.6. See
That said, as for right now, I'm not sure if your extended functions do
the right thing. Maybe someone else can comment.
> -----Original Message-----
> From: Greg Ward [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, July 05, 2000 2:40 PM
> To: [EMAIL PROTECTED]
> Subject: [Zope-dev] 'issubclass' and 'isinstance' on ExtensionClasses
> Hi all --
> surely I'm not the first to deal with this: I use
> 'isinstance()' a lot,
> and 'issubclass()' occasionally, for run-time type-checking. This
> breaks when using ZODB Persistent classes, or any other type of
> ExtensionClass for that matter.
> I see no use of 'issubclass()' in the Zope lib/python directory, and
> only a few scattered uses of 'isinstance()'. (Checked both 2.1.5 and
> 2.2b2.) So maybe I *am* the first to worry about this. I'm surprised
> that ExtensionClass doesn't include enhanced versions of
> and 'isinstance()', though.
> So, here are my versions. Critiques welcome. In particular, is
> "type(X) is not ClassType and hasattr(X, '__bases__')" an adequate
> determination that X is indeed an extension class?
> If you folks want to include these with ExtensionClass (possibly with
> the leading "ec_" trimmed from the names), be my guest.
> def ec_issubclass (class1, class2):
> """A version of 'issubclass' that works with extension classes
> as well as regular Python classes.
> # Both class objects are regular Python classes, so use the
> # built-in 'issubclass()'.
> if type(class1) is ClassType and type(class2) is ClassType:
> return __builtin__.issubclass(class1, class2)
> # Both so-called class objects have a '__bases__' attribute: ie.,
> # they aren't regular Python classes, but they sure look
> like them.
> # Assume they are extension classes and reimplement what
> the builtin
> # 'issubclass()' does behind the scenes.
> elif hasattr(class1, '__bases__') and hasattr(class2,
> # XXX it appears that "ec.__class__ is type(ec)" for an
> # extension class 'ec': could we/should we use this as an
> # additional check for extension classes?
> # Breadth-first traversal of class1's superclass tree. Order
> # doesn't matter because we're just looking for a "yes/no"
> # answer from the tree; if we were trying to resolve a name,
> # order would be important!
> stack = [class1]
> while stack:
> if stack is class2:
> return 1
> del stack
> return 0
> # Not a regular class, not an extension class: blow up
> for consistency
> # with builtin 'issubclass()"
> raise TypeError, "arguments must be class or
> ExtensionClass objects"
> # ec_issubclass ()
> def ec_isinstance (object, klass):
> """A version of 'isinstance' that works with extension classes
> as well as regular Python classes."""
> if type(klass) is ClassType:
> return isinstance(object, klass)
> elif hasattr(object, '__class__'):
> return ec_issubclass(object.__class__, klass)
> return 0
> # ec_isinstance ()
> Greg Ward - software developer [EMAIL PROTECTED]
> MEMS Exchange / CNRI voice: +1-703-262-5376
> Reston, Virginia, USA fax: +1-703-262-5367
> Zope-Dev maillist - [EMAIL PROTECTED]
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope )
Zope-Dev maillist - [EMAIL PROTECTED]
** No cross posts or HTML encoding! **
(Related lists -