[Zope] Re: accessing object from a list constructed in __init__.py

Tres Seaver tseaver at palladion.com
Sun Jul 23 19:09:52 EDT 2006

Hash: SHA1

kevin7kal wrote:
> That did not work for me either.
> It was also suggested that I try inheriting from folder rather than
> re-inventing folder.
> Still no dice.
> I think there is something that I don't understand about acquisition and
> context.  Why can an object be placed into context if it is stored as a
> simple attribute of another object, but not if it is an element in a
> list?  Or what must be done differently when the objects are elements in
> a list?

The piece of the picture you are missing is that the magic of
acquisition happens in the 'getattr' hook:  acquisition-aware objects
create the wrapper (via '__of__') for objects fetched from them via
attribute access.

Containers which want to provide the same semantics for 'getitem' access
have to call '__of__' directly themselves.  For objects derived from
OFS.ObjectManager, that responsibility is delegated to the '_getOb'
method.  Standard folders store their items as attributes, and so their
'_getOb' doesn't have to do anything special::

    def _getOb(self, id, default=_marker):
        if id[:1] != '_' and hasattr(aq_base(self), id):
            return getattr(self, id)
        if default is _marker:
            raise AttributeError, id
        return default

A BTreeFolder, however, does not store items as attributes;  its
'_getOb' looks like::

    def _getOb(self, id, default=_marker):
        """Return the named object from the folder.
        tree = self._tree
        if default is _marker:
            ob = tree[id]
            return ob.__of__(self)
            ob = tree.get(id, _marker)
            if ob is _marker:
                return default
                return ob.__of__(self)

- --
Tres Seaver          +1 202-558-7113          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the Zope mailing list