[Zope] Zope and Polymorphism

Steve Spicklemire steve@spvi.com
Thu, 10 Feb 2000 23:35:12 -0500 (EST)


Hi Johnothan,

   I think the problem with these (issubclass, isinstance) is that
they deal with class objects, and they may not be that easy to handle
in dtml. On the other hand, meta_types are just strings...  so they
can be easily created/changed. I still like the idea of simply
defining some property in the base class that I can quickly check for
in any instance

i.e.,

<dtml-in objectValues>
<dtml-if "_.hasattr(_['sequence-item'],'iCanDoTheChaCha')">

blah blah blah....

</dtml-if>
</dtml-in>


but if you're dead set against that I don't understand why 
you couldn't use something like:

<dtml-in objectValues>
<dtml-if "checkObjectMetaType(_['sequence-item'], metaTypeToCheck)">

blah blah blah....

</dtml-if>
</dtml-in>


where 'metaTypeToCheck' could be "Dokument" as mentioned in another
post, and 'checkObjectMetaType' is defined as below.

-steve

----------------------------------------------------------------------

#
#
# Check a classes base types for a certain meta_type... a little
# safer this time.....
#
#

def checkClassMetaType(theClass, meta_type):
    """ check a class... and all super classess for meta_type...."""

    result = 0

    if hasattr(theClass, 'meta_type') and theClass.meta_type == meta_type:
        result = 1
    else:
        for subClass in theClass.__bases__:
            result = checkClassMetaType( subClass, meta_type)
            if result:
                break

    return result


def checkObjectMetaType(self, object, meta_type):
    """ check and object for an ancestral meta_type..."""

    try:
        if hasattr(object, 'meta_type') and object.meta_type == meta_type:
            return 1
        else:
            return checkClassMetaType(object.__class__, meta_type)
    except AttributeError:
        return 0


----------------------------------------------------------------------
>>>>> "Jonothan" == Jonothan Farr <jfarr@real.com> writes:

    >> Isn't there some method like
    >> _.inheritsfrom(_['sequence-item'],'Folder')


    Jonothan> There's issubclass() in Python. Is this what you're
    Jonothan> looking for?

    Jonothan> issubclass (class1, class2) Return true if class1 is a
    Jonothan> subclass (direct or indirect) of class2. A class is
    Jonothan> considered a subclass of itself. If either argument is
    Jonothan> not a class object, a TypeError exception is raised.

    Jonothan> -jfarr