Python 3 Patterns & Idioms / html / Messenger.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Messenger/Data Transfer Object &mdash; Python 3 Patterns, Recipes and Idioms</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/favicon.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
    <link rel="next" title="Part III: Patterns" href="Part3.html" />
    <link rel="prev" title="A Canonical Form for Command-Line Programs" href="CanonicalScript.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Part3.html" title="Part III: Patterns"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="CanonicalScript.html" title="A Canonical Form for Command-Line Programs"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="messenger-data-transfer-object">
<span id="index-10"></span><h1>Messenger/Data Transfer Object<a class="headerlink" href="#messenger-data-transfer-object" title="Permalink to this headline"></a></h1>
<p>The <em>Messenger</em> or <em>Data Transfer Object</em> is a way to pass a clump of
information around. The most typical place for this is in return values from
functions, where tuples or dictionaries are often used. However, those rely on
indexing; in the case of tuples this requires the consumer to keep track of
numerical order, and in the case of a <strong>dict</strong> you must use the <tt class="docutils literal"><span class="pre">d[&quot;name&quot;]</span></tt>
syntax which can be slightly less desireable.</p>
<p>A Messenger is simply an object with attributes corresponding to the names of
the data you want to pass around or return:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Messenger/MessengerIdiom.py</span>

<span class="k">class</span> <span class="nc">Messenger</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="o">=</span> <span class="n">kwargs</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">Messenger</span><span class="p">(</span><span class="n">info</span><span class="o">=</span><span class="s">&quot;some information&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;list&#39;</span><span class="p">])</span>
<span class="n">m</span><span class="o">.</span><span class="n">more</span> <span class="o">=</span> <span class="mf">11</span>
<span class="k">print</span> <span class="n">m</span><span class="o">.</span><span class="n">info</span><span class="p">,</span> <span class="n">m</span><span class="o">.</span><span class="n">b</span><span class="p">,</span> <span class="n">m</span><span class="o">.</span><span class="n">more</span>
</pre></div>
</div>
<p>The trick here is that the <tt class="docutils literal"><span class="pre">__dict__</span></tt> for the object is just assigned to the
<strong>dict</strong> that is automatically created by the <tt class="docutils literal"><span class="pre">**kwargs</span></tt> argument.</p>
<p>Although one could easily create a <tt class="docutils literal"><span class="pre">Messenger</span></tt> class and put it into a library
and import it, there are so few lines to describe it that it usually makes more
sense to just define it in-place whenever you need it &#8211; it is probably easier
for the reader to follow, as well.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/cover.png" alt="Logo"/>
            </a></p>
    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
    

            <h4>Previous topic</h4>
            <p class="topless"><a href="CanonicalScript.html"
                                  title="previous chapter">A Canonical Form for Command-Line Programs</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="Part3.html"
                                  title="next chapter">Part III: Patterns</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/Messenger.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
    
	  <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
	      <p style="font-size: 90%">Enter search terms or a module, class or function name.</p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>

        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Part3.html" title="Part III: Patterns"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="CanonicalScript.html" title="A Canonical Form for Command-Line Programs"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
      Last updated on Jan 22, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
    </div>
  </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.