When I started rewriting the API for addons.mozilla.org, my views were mostly the same: get some data and render it as either JSON or XML. I also wanted all my API methods to take an api_version
parameter, so I decided class based views would be best. This way my classes could just inherit from a base class.
To do this I had to implement a __call__
method. This works fine, except I wanted to store things into the class – after all the whole point of my use of classes was to keep the code a bit more compact, and cleaner. So, why pass the api_version around everywhere? Unfortunately thread-safety comes to play, and you need a separate instance of your class for each request.
λ
Django’s urlpatterns
expects a callable object. So you can’t give it an instance of AddonDetailView()
. But you could give it a callable that creates an instance of AddonDetailView()
and passes it *args
and **kwargs
. Luckily python has lambda
functions. You can note how we solved that in our urlpatterns
.
λ λ
But wrapping all your urls with lambda
is tedious and remembering to pass *args
and **kwargs
is error prone.
So let’s make a lambda
function that returns… a lambda
function that turns an instance of our class into a callable.
We can now return to coding and not think about thread safety.
λλλ