After giving it more thought:

The supposed View class is a pure utility class that is close to a
module. It does not represent a state in an application, it takes
input and returns output, none of it should outlive a "__call__" call.
Therefore I see no reason for a proper implementation to not be
thread-safe. Just treat "self" as immutable past "__init__". In other
words, don't treat "self" as a junkyard for temporary variables.

This is WRONG:

class View(object):
    def greet(self):
        return HttpResponse('Hi %s' % self.request.POST.get('hello', ''))
    def __call__(self, request):
        self.request = request
        return greet(self)

This is CORRECT and not any harder to design/implement:

class View(object):
    def greet(self, request):
        return HttpResponse('Hi %s' % request.POST.get('hello', ''))
    def __call__(self, request):
        return greet(self, request)

I really don't think we should add anti-moron filters at the framework
level past documenting "taking care to ensire immutability of view
instances".

A true moron will always outsmart us with creativity. It's perfectly
possible to write thread-unsafe code without using classes:

req = None

def greet(self, request):
    global req
    req = request
    # ...
    return HttpResponse('Hi %s' % req.POST.get('hello', ''))

or even:

def greet(self, request):
    greet.foo = request
    # ...
    return HttpResponse('Hi %s' % greet.foo.POST.get('hello', ''))

:)

-- 
Patryk Zawadzki

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@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