[Zope-dev] __getattr__ and Acquisition

Lennart Regebro lennart@torped.se
Mon, 23 Sep 2002 10:35:59 +0200


From: "Andreas Kostyrka" <andreas@kostyrka.priv.at>
> Nope it is not. In a normal method you can access something acquired
> like this:
> self.acquired_value.

I often do just that. :-) I'm not 100% sure what you need to include for
that to work, but I would expect that it is Aqcuisition.Implicit.

> The problem is, that __getattr__ does not have access to the acquisition
> chain.

Well, it does, but it typically causes infinite recursion if you aren't
careful. Something like this might work (this is untested code, adapted from
something that is a bit more complex that we are doing):

    def __getattr__(self, name, marker=None):
        if not name.startswith('_') and not name.startswith('aq_'):
            if not name in self._v_ignore_attrib:
                subob = getattr(self,name,_marker)
                    self._v_ignore_attrib=[]
                    # Return it in context of self, forgetting
                    # its location and acting as if it were located
                    # in self.
                    return aq_base(subob)
                else:
                    self._v_ignore_attrib.append(name)

        raise AttributeError, name


> Basically I want "Transparent Folders" which get their "transparent
> values from the Acquisition path.

Aha. Well, I wouldn't call it trivial, but it is definitely doable.

> Well, I just had an idea for an implementation:
> Third idea: I return some callable object the was generated specifically
> for the access from the __getattr__. This is than executed with
> Acquisition enabled, so it can try to get the real one, ....
> Problem: What if the attribute does not exist? I do not thing that Zope
> reacts to well when instead of a AttributeError exception it gets some
> object that raises AttributeError when accessed, ....
>
> Andreas
>
>