0

I have a site product with a code like this:

class AClass(grok.View):
    """ Code of AClass """
    pass

class BClass(AClass):
    """ Code of BClass with 'update' method defined """
    pass

class CClass(BClass):
    def update(self):
        self.panel = BClass(self.context, self.request)
        # more code     

My doubt is why BClass is instantiate/called in CClass code with two parameters (self.context and self.request). BClass has a update method without other parameters (just self) and doesn't have a __init__ method explicitly. So, what's the function of self.context and self.request in this case? Is this a kind of inheritance or acquisition?

After that I saw this, I think so that I didn't fully understand the omnipresent concepts of context and container in Plone.

Leonardo Andrade
  • 2,508
  • 2
  • 18
  • 24

1 Answers1

3

It makes no sense to both subclass from BClass and have a BClass as an instance. I don't understand what the line self.panel = BClass(self.context, self.request) is supposed to achieve.

context and container are in no way omnipresent. The context is the object which you are viewing. It is set in the classes __init__ method. The container is an attribute of the context, typically __parent__.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • Thanks, @Lennart Regebro! I was never instantiate a grok View with parameters before. I took a look at the grok Documentation (http://grok.zope.org/doc/1.5/reference/components.html#grok.View) and saw that `grok.View` really accepts `self.context` and `self.request` as 'constructor' parameters. Normally I always instantiate a `grok.View` without parameters and never worked with this case of instantiate a view inside another. Is this a good pattern? Thanks so much and sorry for my unclear question. – Leonardo Andrade May 22 '13 at 11:47
  • 2
    @LeonardoAndrade: It is very rare that you need to instantiate views at all. My guess is that whenever you instantiate a view you are doing something wrong. Views are instantiated on objects by the publisher. – Lennart Regebro May 23 '13 at 05:39