[Zope] Howto use getattr() without acquisition?

Gilles Lenfant gilles@pilotsystems.net
Tue, 31 Dec 2002 18:02:05 +0100


I had a similar problem some weeks ago and Dieter Maurer gave me the
enlightenment.

I think you should use getattr(object.aq_explicit, 'someattr')

Hereafter is the copy/paste of his post.

You may want to read the "Acquisition" section of

  <http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html>

You will learn that acquisition wrappers have two components: "aq_self"
and "aq_parent", and that an implicit acquisition wrapper first
asks "aq_self" and, if unsuccessful, automatically asks "aq_parent"
for an attribute lookup, while an explicit wrapper only asks "aq_self".

With these explanations you will understand that
"hasattr(obj.aq_explicit,*)"
is equivalent to "hasattr(obj.aq_base,*)" iff "obj.aq_self" is not
itself acquisition wrapped.


Dieter



----- Original Message -----
From: dave@loewen.com
To: zope@zope.org
Sent: Tuesday, December 31, 2002 5:08 PM
Subject: [Zope] Howto use getattr() without acquisition?



I'm building a python script to generate breadcrumb navigation links on each
page of my website.

It creates hyperlinks and uses the title or id for the link. I'd like to add
a feature so that if the folder has a "nickname" property, it will use this
for the breadcrumb hyperlink instead of the title.

I tried using "hasattr()" to see if the current folder has a nickname
property. It seems to work if the folder has a nickname property. However,
if the folder doesn't have that properly, but the parent does, it still
returns "true" and then getattr() acquires it from the parent instead of
using the title. Is there a way to tell hasattr() to *not* use acquisition?
Or perhaps another method which would do what I want? I'm sure there is a
much more elegant way to do this...

(These question feels like a FAQ, and I did do my best to search the group
archives, but couldn't find anything appropriate-- most breadcrumb
conversations seem to revolve around DTML and getProperty().)

--dave


"""
This script creates breadcrumb style navigation links.

It walks the REQUEST.PARENTS list of parents and creates
a hyperlink for each parent. It stops at the Examples folder.
"""
links=[]
for parent in context.REQUEST.PARENTS[1:]:
    id = parent.getId()
    if hasattr(parent, 'nickname'):
        label = getattr(parent, 'nickname')
    elif parent.title != "":
        label = parent.title
    else:
        label = parent.getId()
    links.insert(0, """<a href="%s" class="breadcrumb">%s</a>""" %
(parent.absolute_url(), label))

# Now get label for current container
if context.hasProperty('nickname'):
  label = context.nickname
elif context.title != "":
  label = context.title
else:
  label = context.getId()

return " : ".join(links) + " : " + label