RE: [Zope] Zope and Polymorphism
yes but what if you don't own the base class?
-----Original Message----- From: Steve Spicklemire [mailto:steve@spvi.com] Sent: Friday, February 11, 2000 3:35 PM To: jfarr@real.com Cc: djay@lucent.com; zope@zope.org Subject: Re: [Zope] Zope and Polymorphism
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
Hi Jay, No problem! You've just got to use the second form... (or you could always add the same property to all your classes that inherit from that base class, so long as your not interested in 'raw' instances of the base class as well.). But the second form, using the external method supplied, should work in all cases, though it will be somewhat slower, since searching through the class hierarchy is more time consuming that checking a single instance variable. -steve
"Jay," == Jay, Dylan <djay@lucent.com> writes:
Jay,> yes but what if you don't own the base class?
participants (2)
-
Jay, Dylan -
Steve Spicklemire