On Fri, 9 Nov 2012 14:05:01 +0800
Russell Keith-Magee <russ...@keith-magee.com> wrote:

> Why on earth can't just just take the logic that you're putting in init(),
> and put it *in dispatch()*. The sequence of calls is *identical*, and since
> *args and **kwargs are locals, they don't need to be set anywhere. What's
> the problem with putting the init() logic in the dispatch() method in the
> way I described?

I think the problem is that order of initialization _is_ reversed when
inheritance comes into the game, in the case of handling everything at
the beginning of dispatch(). Consider:

class DepartmentMixin(object):
    def initialize(self, request, *args, **kwargs):
        super(DepartmentMixin, self).initialize(request, *args, **kwargs)
        self.department = get_object_or_404(Department, 
slug=kwargs['department'])

class ShiftMixin(DepartmentMixin):
    def initialize(self, request, *args, **kwargs):
        super(ShiftMixin, self).initialize(request, *args, **kwargs)
        self.shift = get_object_or_404(Shift, department=self.department, 
datetime__year=kwargs['year'])

This would not be possible in dispatch() because the super method must
be called at the end of the child method (tail recursion):

class DepartmentMixin(object):
    def dispatch(self, request, *args, **kwargs):
        self.department = get_object_or_404(Department, 
slug=kwargs['department'])
        return super(DepartmentMixin, self).dispatch(request, *args, **kwargs)

class ShiftMixin(DepartmentMixin):
    def initialize(self, request, *args, **kwargs):
        # THIS DOESN'T WORK: self.department HAS NOT BEEN SET YET.
        self.shift = get_object_or_404(Shift, department=self.department, 
datetime__year=kwargs['year'])
        return super(ShiftMixin, self).dispatch(request, *args, **kwargs)

I think the control flow is easier to follow in case of initialize()
vs. dispatch(). This doesn't mean that I think initialize() should be
part of core, but the described situation is something I came across in
real life.

Best wishes,
Sebastian.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to