Browser views: inherit from object or BrowserView from Five/Plone

Maurits van Rees m.van.rees at zestsoftware.nl
Thu Apr 19 09:54:15 UTC 2007


Martin Aspeli, on 2007-04-18:
> from Acquisition import aq_inner
> from Products.Five.browser import BrowserView
>
> class MyView(BrowserView):
>
>     def some_method(self):
>         context = aq_inner(self.context)
>         # use context in some way

One thought: if a context is already acquisition wrapped, you could
instead say `self.context.aq_inner`.  That would save one import.
Maybe not really worth troubling myself over, but am I correct in
assuming that it is better to do that import anyway, like you write?

It would at least avoid AttributeErrors when the context does not
inherit from Acquicition for some reason.

(Pdb) object().aq_inner
*** AttributeError: 'object' object has no attribute 'aq_inner'
(Pdb) from Acquisition import aq_inner, aq_chain
(Pdb) aq_inner(object())
<object object at 0xb7e31700>
(Pdb) aq_chain(aq_inner(object()))
[<object object at 0xb7e31708>]

As an example, I see some code like this in my product, in the content
classes themselves:

    current = portal.aq_inner
    parent = self.aq_inner.aq_parent

Is it better to avoid that and import aq_* from Acquisition
and change the code into this?

    current = aq_inner(portal)
    parent = aq_parent(aq_inner(self))


-- 
Maurits van Rees | http://maurits.vanrees.org/ [NL]
            Work | http://zestsoftware.nl/
"Do not worry about your difficulties in computers,
 I can assure you mine are still greater."





More information about the Product-Developers mailing list