[Zope-CMF] How to get Type in manage_afterAdd?

Dieter Maurer dieter@handshake.de
Sat, 2 Feb 2002 11:56:14 +0100


Jon Edwards writes:
 > I did some reading and I understand now that aq_base returns just the
 > object, with no acquisition. But I'm a little confused about aq_explicit :-)
When you have an aquisition wrapped object, it has two components,
"aq_self" and "aq_parent". When you ask the object for an attribute,
it asks "aq_self" for it. If this fails, then it depends whether
or not the object is an explicit or an implicit acquisition wrapper
(in the current Zope, most objects are implicitly wrapped).
For an implicit wrapper, "aq_parent" is automatically asked
for the attribute.

 > Could anyone give an example where aq_explicit might not do what I want in
 > this context (I'm checking to see if a folder already has an 'index.htm'
 > document before adding it)?
It is not so easy to construct an example that fails in your context
(it may be impossible). The problem may show up, when
you access your folder in a way where acquisition is used several
times, say you have:

       folder/
         your_special_folder
	 some_other_folder

	 your_document

and in "your_document" you access "some_other_folder.your_special_folder",
then what you get is an acquisition wrapped object whose
"aq_self" is again acquisition wrapped. This second level acquisition is
not disabled by use of "aq_explicit".

There are lots of additional acquisition magic that may ensure
that your specific case will nevertheless work. For example,
when you call a method of an object, then the acquistion context
is sometimes stripped a bit. But their may be some cases,
where you may get surprised...

When you are interested, you may search the mailing list archives
for a post from me several years ago, where I report that I have
been surprised by the "aq_explicit" in "DTMLDocument.__call__".

When you want to know whether an object "obj" has an attribute "attr", then
the following piece of code is safe:

    from Acquistion import aq_base
    obj= aq_base(obj)
    return hasattr(obj,attr)

In many cases, you can use "aq_explicit" but in some rare cases
you may be hit by unexpected behaviour.

It's up to you whether route you want to go...


Dieter