On Thursday 17 October 2013 08:34:48 Aymeric Augustin wrote:
> 
> For instance, thread locals are strictly equivalent to regular variables in
> tests because they are single threaded (with a handful of exceptions). But
> allowing testing in isolation is a major goal of "removing global state".

If I understand correctly, what Jonathan is suggesting is not your garden-
variety thread-local variables, but what Common Lisp calls "dynamically scoped 
variables" -- you could call them "call-stack-local" or something like that. 
In Python terms, every change to the values of such variables would be done in 
the __enter__() of a context manager, and undone (that is, old value restored) 
in the __exit__(). I think such variables are a great idea, and would indeed 
help a lot with all the problems associated with global state. In terms of 
software engineering, they are a lot like exceptions: A channel of 
communications between functions on different levels of the call stack, that 
does not require explicit acknowledgement on every level, and yet does not 
completely break locality.

However, I don't think such variables can be used in a reliable and elegant 
manner without language-level support, and sadly, Python does not support 
them. Attempts I've made to get the functionality using context managers ended 
with awkward APIs for either setting values, getting values, or both; as a 
trivial example, Jonathan's suggestion of the context manager being returned 
by django_project.activate() is of such low granularity, that it is almost 
equivalent to regular globals.

If a reasonable API for this can be defined, I'd be all for it. I suspect 
that's a non-trivial "if".

Shai.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/3631394.jhQ5yuEHHh%40deblack.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to