[Zope-dev] 'issubclass' and 'isinstance' on ExtensionClasses

Chris McDonough chrism@digicool.com
Wed, 5 Jul 2000 14:50:38 -0400


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
http://starship.python.net/crew/amk/python/writing/new-python/new-python
.html#SECTION000900000000000000000

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:gward@mems-exchange.org]
> Sent: Wednesday, July 05, 2000 2:40 PM
> To: zope-dev@zope.org
> 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 
> 'issubclass()'
> 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.
> 
> --snip 
> snip-------------------------------------------------------------
> 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, 
> '__bases__'):
>         # 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[0] is class2:
>                 return 1
>             stack.extend(list(stack[0].__bases__))
>             del stack[0]
>         else:
>             return 0
> 
>     # Not a regular class, not an extension class: blow up 
> for consistency
>     # with builtin 'issubclass()"
>     else:
>         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)
>     else:
>         return 0
>     
> # ec_isinstance ()
> --snip 
> snip-------------------------------------------------------------
> 
> -- 
> Greg Ward - software developer                gward@mems-exchange.org
> MEMS Exchange / CNRI                           voice: +1-703-262-5376
> Reston, Virginia, USA                            fax: +1-703-262-5367
> 
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope-dev
> **  No cross posts or HTML encoding!  **
> (Related lists - 
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope )
>