Best way to change the value of "settings" from within a Python test case?
Question: I'm writing unit tests in Python for the first time, for a Django app. I've
struck a problem. In order to test a particular piece of functionality, I need
to change the value of one of the app's settings. Here's my first attempt:

    
    
    def test_in_list(self):
        mango.settings.META_LISTS = ('tags',)
        tags = Document(filepath).meta['tags']
        self.assertEqual(tags, [u'Markdown', u'Django', u'Mango'])
    

What I'm trying to do is change the value of `META_LISTS` such that the new
value is used when the `Document` object is created. The relevant imports
are...

    
    
    # tests.py
    from mango.models import Document
    import mango.settings
    
    # models.py
    from mango.settings import *
    

If I've understood correctly, since `models.py` has already imported the names
from `mango.settings`, changing the value of `META_LISTS` within
`mango.settings` will not alter the value of `META_LISTS` within
`mango.models`.

It's possible – likely even – that I'm going about this in completely the
wrong way. What's the correct way to alter the value of such a "setting" from
within a test case?

**Edit:** I failed to mention that the file `models.py` contains vanilla
Python classes rather than Django models. I certainly need to rename this
file!


Answer: In models.py, use `import mango.settings`. You can then set a variable in your
test code like you would any other:

    
    
    mango.settings.foo = 'bar'
    

A module is a singleton. You can change the values in its namespace from
anywhere in your code.

But this won't work if you use `from mango.settings import *`, since that
expression copies the values in the module into the current namespace.

