Loading ...
Sorry, an error occurred while loading the content.

Re: [Cheetahtemplate-discuss] More subclassing blues...

Expand Messages
  • Mike Orr
    ... The child s __init__ needs to call all its parents __init__ methods explicitly, like this: def __init__(self): # ... some code MixinClass.__init__(self)
    Message 1 of 8 , Feb 7, 2002
    • 0 Attachment
      On Thu, Feb 07, 2002 at 08:54:55AM +0100, Magnus Lie Hetland wrote:
      > > __init__ in a mixin is difficult to deal with in most situations -- not
      > > just Cheetah -- but I suppose worse in Cheetah since the template's
      > > __init__ is important and relatively opaque.
      >
      > Yes... But (as I'm sure you've understood) my problem wasn't that the
      > mixin's __init__ wasn't called -- it was the standard behaviour (the
      > template's __init__ being called) that disappeared, so nothing worked
      > anymore.

      The child's __init__ needs to call all its parents' __init__ methods
      explicitly, like this:

      def __init__(self):
      # ... some code
      MixinClass.__init__(self)
      Template.__init__(self, ... arguments ... )
      # ... more code

      > > You could solve this by creating yet another class that called both
      > > __init__'s. This, of course, is not at all pretty. It's better to try
      > > to find a way not to need __init__.

      But oftentimes you can't avoid the __init__ without getting even more
      convoluted. Sometiems youc an avoid it by defining attributes at the
      class level. The methods won't know the diference, and if you reasssign
      them later, it'll seamlesslly create instance attributes. But don't try to
      modify mutable objects in place without explicitly copying them to
      instance variables, or you'll end up modifying the shared class copy.


      --
      -Mike (Iron) Orr, iron@... (if mail problems: mso@...)
      http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol

      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Ian Bicking
      ... Hmm... I think I had misremembered how I had done this before. I did put the template first and the mixin seperate -- but the mixin didn t override
      Message 2 of 8 , Feb 7, 2002
      • 0 Attachment
        On Thu, 2002-02-07 at 01:54, Magnus Lie Hetland wrote:
        > Ian Bicking <ianb@...>:
        > >
        > [...]
        > >
        > > The Mixin should be very minimal. So, for instance, if you want to
        > > implement awake and sleep, *just* implement those methods.
        >
        > Well, that's what I tried. It didn't work in the obvious manner...
        > I.e. if I wrote a template and did my subclassing with
        >
        > #extends FooMixin, FooTemplate
        >
        > things wouldn't work. WebKit would complain that my instance lacked
        > all kinds of attributes -- and the reason is that the compiler
        > subclass template only called the __init__ of FooMixin.
        >
        > Why nothing works when I subclass Template directly (with no mixins) I
        > still haven't figured out.

        Hmm... I think I had misremembered how I had done this before. I did
        put the template first and the mixin seperate -- but the mixin didn't
        override anything that the template defined.

        > As I said, the solution I found was something like this:
        >
        > Template
        > A
        > | [Implicit inheritance]
        > SiteStyle [A Cheetah Template]
        > A
        > | [Explicit inheritance]
        > SiteLogic [Tiny Mixin that calls the apropriate awake and sleep
        > A methods]
        > | [Explicit inheritance]
        > SiteTemplate [Cheetah template]
        > A
        > | [Explicit inheritance]
        > index.tmpl [Some page]
        >
        > Excuse the ASCII art ;)

        You could also do:

        Template -> SiteLogic -> SiteStyle -> index.tmpl

        hmm... no, inheriting from Template is odd, maybe:

        Template -> SiteStyle -> SiteTemplate (a python file which contains the
        logic from SiteLogic) -> index.tmpl

        This should work, and you won't need multiple inheritance -- it's always
        easiest to avoid multiple inheritance.

        Ian



        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      • Mike Orr
        ... I wasn t thinking this morning. The easiest way to do this is to have initialization methods in your mixin classes that are named something other than
        Message 3 of 8 , Feb 7, 2002
        • 0 Attachment
          On Thu, Feb 07, 2002 at 08:07:47AM -0800, Mike Orr wrote:
          > > > You could solve this by creating yet another class that called both
          > > > __init__'s. This, of course, is not at all pretty. It's better to try
          > > > to find a way not to need __init__.

          > But oftentimes you can't avoid the __init__ without getting even more
          > convoluted. Sometiems youc an avoid it by defining attributes at the
          > class level. The methods won't know the diference, and if you reasssign
          > them later, it'll seamlesslly create instance attributes. But don't try to
          > modify mutable objects in place without explicitly copying them to
          > instance variables, or you'll end up modifying the shared class copy.

          I wasn't thinking this morning. The easiest way to do this is to have
          initialization methods in your mixin classes that are named something
          other than .__init__. Then call them explicitly from the template:
          #silent $mixin1Init(...args...)
          #silent $mixin2Init

          Or if you prefer to avoid multiple inheritance like Ian suggested,
          just have every class' .__init__ call its parent's .__init__.

          --
          -Mike (Iron) Orr, iron@... (if mail problems: mso@...)
          http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol

          _______________________________________________
          Cheetahtemplate-discuss mailing list
          Cheetahtemplate-discuss@...
          https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
        Your message has been successfully submitted and would be delivered to recipients shortly.