Sandboxing / copying a module in two separate places to prevent overwriting or monkey patching
<p>Using these four files, all in the same directory:</p>

<h3>splitter.py</h3>

<pre><code>#imagine this is a third-party library
SPLIT_CHAR = ','

class Splitter(object):
    def __init__(self, s, split_char=None):
        self.orig = s
        if not split_char:
            self.splitted = s.split(SPLIT_CHAR)
</code></pre>

<h3>a.py</h3>

<pre><code>#this person makes the mistake of re-setting the global variable
#in splitter to get different behavior, instead of just passing
#in the extra argument
import splitter

splitter.SPLIT_CHAR = '|'

def go():
    s1 = splitter.Splitter("a|b|c|d")
    print s1.orig
    print s1.splitted
</code></pre>

<h3>b.py</h3>

<pre><code>#this person expects the default behavior (splitting commas)
from splitter import Splitter

def go():
    s1 = Splitter('a,b,c,d')
    print s1.orig
    print s1.splitted
</code></pre>

<h3>experiment.py</h3>

<pre><code>import a
import b

a.go() #this one sets the global var in splitter
b.go() #this one expects the default behavior
</code></pre>

<p>The output of <code>experiment.py</code> will be:</p>

<pre><code>a|b|c|d
['a', 'b', 'c', 'd'] #okay... everything is fine
a,b,c,d
['a,b,c,d'] #not what the programmer expected.. should be split on commas
</code></pre>

<p>Is there a way to prevent this result? In this case, maybe <code>a.py</code> and <code>b.py</code> are written by two different coworkers, yet A's code is affecting B. It can sometimes be useful to overwrite something in a module (e.g. monkeypatching), but in this case it's producing confusing behavior.</p>

<p>Is there some way to make a copy of the module or sandbox the execution so that <code>a.py</code> can't overwrite values in <code>splitter.py</code> and end up affecting <code>b.py</code>?</p>

<p>Also, let's say instead of the simple <code>a.py</code> and <code>b.py</code>, we are running a hundred web apps under mod_python or something (sharing as much as possible to decrease memory), is there a way to prevent one rogue app from tinkering with a module and breaking all the other apps? Something like Google App Engine has this solved, of course :)</p>
