Source

dictionary_switches / html / singleton.html

Full commit
<!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>The Singleton &mdash; Python 3 Patterns & 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'
      };
    </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 & Idioms" href="index.html" />
    <link rel="next" title="Building Application Frameworks" href="appFrameworks.html" />
    <link rel="prev" title="The Pattern Concept" href="patternConcept.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="appFrameworks.html" title="Building Application Frameworks"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="patternConcept.html" title="The Pattern Concept"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns & Idioms</a> &raquo;</li>
      </ul>
    </div>
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="the-singleton">
<h1>The Singleton<a class="headerlink" href="#the-singleton" title="Permalink to this headline"></a></h1>
<p>Possibly the simplest design pattern is the <em>singleton</em>, which is a way to
provide one and only one object of a particular type. To accomplish this, you
must take control of object creation out of the hands of the programmer. One
convenient way to do this is to delegate to a single instance of a private
nested inner class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/SingletonPattern.py</span>

<span class="k">class</span> <span class="nc">OnlyOne</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">__OnlyOne</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="n">arg</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">arg</span>
        <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="sb">`self`</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span>
    <span class="n">instance</span> <span class="o">=</span> <span class="bp">None</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="n">arg</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span><span class="p">:</span>
            <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="o">.</span><span class="n">__OnlyOne</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">arg</span>
    <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">(</span><span class="s">&#39;sausage&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">(</span><span class="s">&#39;eggs&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">(</span><span class="s">&#39;spam&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`x`</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`y`</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`z`</span><span class="p">)</span>
<span class="n">output</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0076B7AC&gt;sausage</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0076B7AC&gt;eggs</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam</span>
<span class="s">&lt;__main__.OnlyOne instance at 0076C54C&gt;</span>
<span class="s">&lt;__main__.OnlyOne instance at 0076DAAC&gt;</span>
<span class="s">&lt;__main__.OnlyOne instance at 0076AA3C&gt;</span>
<span class="s">&#39;&#39;&#39;</span>
</pre></div>
</div>
<p>Because the inner class is named with a double underscore, it is private so the
user cannot directly access it. The inner class contains all the methods that
you would normally put  in the class if it weren&#8217;t going to be a singleton, and
then it is wrapped in the outer class which controls creation by using its
constructor. The first time you create an <strong>OnlyOne</strong>, it initializes
<strong>instance</strong>, but after that it just ignores you.</p>
<p>Access comes through delegation, using the <strong>__getattr__( )</strong> method to redirect
calls to the single instance. You can see from the output that even though it
appears that multiple objects have been created, the same <strong>__OnlyOne</strong> object
is used for both. The instances of <strong>OnlyOne</strong> are distinct but they all proxy
to the same <strong>__OnlyOne</strong> object.</p>
<p>Note that the above approach doesn&#8217;t restrict you to creating only one object.
This is also a technique to create a limited pool of objects. In that situation,
however, you can be confronted with the problem of sharing objects in the pool.
If this is an issue, you can create a solution involving a check-out and check-
in of the shared objects.</p>
<p>A variation on this technique uses the class method <strong>__new__</strong> added in Python
2.2:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/NewSingleton.py</span>

<span class="k">class</span> <span class="nc">OnlyOne</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">__OnlyOne</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="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="sb">`self`</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span>
    <span class="n">instance</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span> <span class="c"># __new__ always a classmethod</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span><span class="p">:</span>
            <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="o">.</span><span class="n">__OnlyOne</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">OnlyOne</span><span class="o">.</span><span class="n">instance</span>
    <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">()</span>
<span class="n">x</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;sausage&#39;</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">()</span>
<span class="n">y</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;eggs&#39;</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">OnlyOne</span><span class="p">()</span>
<span class="n">z</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;spam&#39;</span>
<span class="k">print</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="c">#&lt;hr&gt;</span>
<span class="n">output</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0x00798900&gt;sausage</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0x00798900&gt;eggs</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0x00798900&gt;spam</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0x00798900&gt;spam</span>
<span class="s">&lt;__main__.__OnlyOne instance at 0x00798900&gt;spam</span>
<span class="s">&#39;&#39;&#39;</span>
</pre></div>
</div>
<p>Alex Martelli makes the <a class="reference external" href="http://www.aleax.it/Python/5ep.html">observation</a>
that what we really want with a Singleton is to have a single set of state data
for all objects. That is, you could create as many objects as you want and as
long as they all refer to the same state information then you achieve the effect
of Singleton. He accomplishes this with what he calls the <em>Borg</em> <a class="footnote-reference" href="#id4" id="id1">[1]</a>, which is
accomplished by setting all the <strong>__dict__</strong>s to the same static piece of
storage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/BorgSingleton.py</span>
<span class="c"># Alex Martelli&#39;s &#39;Borg&#39;</span>

<span class="k">class</span> <span class="nc">Borg</span><span class="p">:</span>
    <span class="n">_shared_state</span> <span class="o">=</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="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shared_state</span>

<span class="k">class</span> <span class="nc">Singleton</span><span class="p">(</span><span class="n">Borg</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="n">arg</span><span class="p">):</span>
        <span class="n">Borg</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">arg</span>
    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Singleton</span><span class="p">(</span><span class="s">&#39;sausage&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">Singleton</span><span class="p">(</span><span class="s">&#39;eggs&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">Singleton</span><span class="p">(</span><span class="s">&#39;spam&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`x`</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`y`</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="sb">`z`</span><span class="p">)</span>
<span class="n">output</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">sausage</span>
<span class="s">eggs</span>
<span class="s">spam</span>
<span class="s">spam</span>
<span class="s">spam</span>
<span class="s">&lt;__main__.Singleton instance at 0079EF2C&gt;</span>
<span class="s">&lt;__main__.Singleton instance at 0079E10C&gt;</span>
<span class="s">&lt;__main__.Singleton instance at 00798F9C&gt;</span>
<span class="s">&#39;&#39;&#39;</span>
</pre></div>
</div>
<p>This has an identical effect as <strong>SingletonPattern.py</strong> does, but it&#8217;s more
elegant. In the former case, you must wire in <em>Singleton</em> behavior to each of
your classes, but <em>Borg</em> is designed to be easily reused through inheritance.</p>
<p>A simpler version <a class="footnote-reference" href="#id5" id="id2">[2]</a> of this takes advantage of the fact that there&#8217;s only one
instance of a class variable:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/ClassVariableSingleton.py</span>
<span class="k">class</span> <span class="nc">SingleTone</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">__instance</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">SingleTone</span><span class="o">.</span><span class="n">__instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">SingleTone</span><span class="o">.</span><span class="n">__instance</span> <span class="o">=</span> <span class="nb">object</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">)</span>
        <span class="n">SingleTone</span><span class="o">.</span><span class="n">__instance</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">val</span>
        <span class="k">return</span> <span class="n">SingleTone</span><span class="o">.</span><span class="n">__instance</span>
</pre></div>
</div>
<p>Two other interesting ways to define singleton <a class="footnote-reference" href="#id6" id="id3">[3]</a> include wrapping a class and
using metaclasses. The first approach could be thought of as a <em>class decorator</em>
(decorators will be defined later in the book), because it takes the class of
interest and adds functionality to it by wrapping it in another class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/SingletonDecorator.py</span>
<span class="k">class</span> <span class="nc">SingletonDecorator</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="n">klass</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">klass</span> <span class="o">=</span> <span class="n">klass</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">==</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">klass</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>

<span class="k">class</span> <span class="nc">foo</span><span class="p">:</span> <span class="k">pass</span>
<span class="n">foo</span> <span class="o">=</span> <span class="n">SingletonDecorator</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>

<span class="n">x</span><span class="o">=</span><span class="n">foo</span><span class="p">()</span>
<span class="n">y</span><span class="o">=</span><span class="n">foo</span><span class="p">()</span>
<span class="n">z</span><span class="o">=</span><span class="n">foo</span><span class="p">()</span>
<span class="n">x</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;sausage&#39;</span>
<span class="n">y</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;eggs&#39;</span>
<span class="n">z</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="s">&#39;spam&#39;</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">z</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span> <span class="ow">is</span> <span class="n">y</span> <span class="ow">is</span> <span class="n">z</span><span class="p">)</span>
</pre></div>
</div>
<p>[[ Description ]]</p>
<p>The second approach uses metaclasses, a topic I do not yet understand but which
looks very interesting and powerful indeed (note that Python 2.2 has
improved/simplified the metaclass syntax, and so this example may change):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># singleton/SingletonMetaClass.py</span>
<span class="k">class</span> <span class="nc">SingletonMetaClass</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span><span class="n">name</span><span class="p">,</span><span class="n">bases</span><span class="p">,</span><span class="nb">dict</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SingletonMetaClass</span><span class="p">,</span><span class="n">cls</span><span class="p">)</span>\
          <span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="n">bases</span><span class="p">,</span><span class="nb">dict</span><span class="p">)</span>
        <span class="n">original_new</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">__new__</span>
        <span class="k">def</span> <span class="nf">my_new</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">cls</span><span class="o">.</span><span class="n">instance</span> <span class="o">==</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">cls</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> \
                  <span class="n">original_new</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">instance</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">__new__</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">my_new</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">bar</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">SingletonMetaClass</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="n">val</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">val</span>
    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="sb">`self`</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span>

<span class="n">x</span><span class="o">=</span><span class="n">bar</span><span class="p">(</span><span class="s">&#39;sausage&#39;</span><span class="p">)</span>
<span class="n">y</span><span class="o">=</span><span class="n">bar</span><span class="p">(</span><span class="s">&#39;eggs&#39;</span><span class="p">)</span>
<span class="n">z</span><span class="o">=</span><span class="n">bar</span><span class="p">(</span><span class="s">&#39;spam&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span> <span class="ow">is</span> <span class="n">y</span> <span class="ow">is</span> <span class="n">z</span><span class="p">)</span>
</pre></div>
</div>
<p>[[ Long, detailed, informative description of what metaclasses are and how they
work, magically inserted here ]]</p>
<div class="section" id="exercises">
<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline"></a></h2>
<ol class="arabic simple">
<li><strong>SingletonPattern.py</strong> always creates an object, even if it&#8217;s never used.
Modify this program to use <em>lazy initialization</em>, so the singleton object is
only created the first time that it is needed.</li>
<li>Using <strong>SingletonPattern.py</strong> as a starting point, create a class that
manages a fixed number of its own objects. Assume the objects are database
connections and you only have a license to use a fixed quantity of these at
any one time.</li>
<li>Modify <strong>BorgSingleton.py</strong> so that it uses a class <strong>__new__( )</strong> method.</li>
</ol>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id4" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>From the television show <em>Star Trek: The Next Generation</em>. The Borg are
a hive-mind collective: &#8220;we are all one.&#8221;</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>From Dmitry Balabanov.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id6" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td>Suggested by Chih-Chung Chang.</td></tr>
</tbody>
</table>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/Logo.png" alt="Logo"/>
            </a></p>
    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
    
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">The Singleton</a><ul>
<li><a class="reference external" href="#exercises">Exercises</a></li>
</ul>
</li>
</ul>


            <h4>Previous topic</h4>
            <p class="topless"><a href="patternConcept.html" title="previous chapter">The Pattern Concept</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="appFrameworks.html" title="next chapter">Building Application Frameworks</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/singleton.txt">Show Source</a></li>
            </ul>
    
          <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>
    <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="appFrameworks.html" title="Building Application Frameworks"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="patternConcept.html" title="The Pattern Concept"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns & Idioms</a> &raquo;</li>
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
      Last updated on Nov 11, 2008.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
    </div>
  </body>
</html>