On Fri, Jun 4, 2010 at 1:53 PM, Roald <downa...@gmail.com> wrote:
>    class my_view(HttpResponse):
>        __metaclass__ = ABCMeta
>
>        def __init__(self, *args, **kwargs):
>            print 'init my_view'
>
>        def __new__(cls, *args, **kwargs):
>            if some_condition():
>                return some_view(*args, **kwargs)
>            elif other_condition():
>                return some_standard_response(*args, **kwargs)
>            else:
>                ...
>                return object.__new__(cls)
>
>
>    my_view.register(some_view)
>    my_view.register(some_standard_response)
>
>
> This (or something like it) seems to be able to take the best of the
> __new__ and __init__ options.

Uhm... guys,

Maybe something simpler?

---- 8< ----

from threading import local, Thread

class View(object):
    _child = local()

    def __new__(cls, *args, **kwargs):
        existing = hasattr(cls._child, 'instance')
        if not existing:
            print 'first time in this thread'
            cls._child.instance = super(View, cls).__new__(cls, *args, **kwargs)
        return cls._child.instance

    def __call__(self, foo):
        print 'call', id(self), foo
        return 'bar'

test = View()
test(1)
test = View()
test(2)
test = View()
test(3)

class TestThread(Thread):
    def run(self):
        test = View()
        test(4)

t = TestThread()
t.setDaemon(True)
t.start()
t.join()

test = View()
test(5)

---- 8< ----

Try it now, tests itself.

-- 
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