Easily dumping variables from/to namespaces/dictionaries in Python
<p>Say I have several variables or objects in Python, <code>a</code>, <code>b</code>, <code>c</code>, ...</p>

<p>How can I easly <em>dump</em> these variables into a namespace in Python and restore them at a later time?  (e.g. in the same way <code>argparse</code> wraps various variables into a namespace).</p>

<p>Here are two examples of how I would like to dump things to and from a namespace:</p>

<h2>Dumping local variables into a namespace</h2>

<pre><code>function (bar):
   # We start with a, b and c
   a = 10
   b = 20
   c = "hello world"

   # We can dump anything we want into e, by just passing things as arguments:
   e = dump_into_namespace(a, b, c) 
   del a, b, c

   print (e.a + e.b) # Prints 30
   return e  # We can return e if we want. This is just a use case scenario
</code></pre>

<h2>Dumping local variables from a namespace e</h2>

<pre><code># We start with e, which for example was built with a call to 
# dump_into_namespace(a,b,c) somewhere else in the program,
# in which case e would hold a, b and c 

# We may receive e through a function call or load it from disk, e.g.:

function foo(e):

   # The following call creates the variables a,b and c
   # or updates their values if the already exist in memory
   dump_from_namespace(e) 
   del e

   print(a + b) # Prints 30
   print(c) # Prints hello world
</code></pre>

<p>My first question is: <strong>Is this possible at all in Python?</strong> (note that the method <code>dump_into_namespace</code> does not directly receive the names of the variables, at least as far as I can tell). </p>

<p>If the answer to the above is no, how could I do it with an interface like this?</p>

<pre><code>e = dump_into_namespace('a', 'b', 'c')
</code></pre>

<p>Also, how would this be done this with a <strong>dictionary</strong> instead of a <strong>namespace</strong>?</p>

<p>There are a few threads that seem relevant addressing a <strong>dot-access</strong> of dynamically-defined variables, but I don't think they address the problem of dumping variables:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/8306171/python-extract-variables-out-of-namespace-argparse">Python: Extract variables out of namespace (argparse)</a></li>
<li><a href="http://stackoverflow.com/questions/14716727/picklable-data-containers-that-are-dumpable-in-the-current-namespace">Picklable data containers that are dumpable in the current namespace</a></li>
<li><a href="http://stackoverflow.com/questions/13520421/recursive-dotdict">Recursive DotDict</a></li>
<li><a href="http://stackoverflow.com/questions/2352181/how-to-use-a-dot-to-access-members-of-dictionary">How to use a dot &quot;.&quot; to access members of dictionary?</a></li>
<li><a href="http://stackoverflow.com/questions/224026/javascript-style-dot-notation-for-dictionary-keys-unpythonic">Javascript style dot notation for dictionary keys unpythonic?</a></li>
<li><a href="http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python?lq=1">Accessing dict keys like an attribute in Python?</a></li>
<li><a href="http://stackoverflow.com/questions/3031219/python-recursively-access-dict-via-attributes-as-well-as-index-access">Python: Recursively access dict via attributes as well as index access?</a></li>
<li><a href="http://stackoverflow.com/questions/3797957/python-easily-access-deeply-nested-dict-get-and-set?lq=1">Python: Easily access deeply nested dict (get and set)</a></li>
<li><a href="http://stackoverflow.com/questions/5021041/are-there-any-gotchas-with-this-python-pattern?lq=1">Are there any &#39;gotchas&#39; with this Python pattern?</a></li>
</ul>

<p>Are there any libraries that facilitate this type of access through dot notation?</p>

<h3>Update:</h3>

<p>It looks like there is a library that supports dot-accessible dictionaries in Python, called <a href="http://pypi.python.org/pypi/bunch" rel="nofollow">Bunch</a>, but I am not sure it would support easily dumping as I defined it.</p>
