title: How to create a metaclass that can give a class an array of instances and provide a "voodoo" instance that acts on all class instances?
body: <p>I'm wondering how to create a metaclass in Python that can create other classes that:</p>
  
  <ul>
  <li>Store their instances in an array automatically</li>
  <li>Have a special instance, <code>NonMetaClass.all</code>, whose properties:
  
  <ul>
  <li>When set, set all the class's instances with the same key to the same value (e.g., <code>Foo.all.num = 3</code> makes all instances of <code>Foo</code> have a <code>num</code> of 3)</li>
  <li>When accessed (get), returns an array of all of the class's instances's key values (e.g., <code>Foo.all.num</code> returns <code>[5, 3, 2]</code>)</li>
  <li>Cannot be deleted.</li>
  <li>When called (if the attribute is a function), call that method on all the instances of a class.</li>
  </ul></li>
  </ul>
  
  <p>In Python terms, I would like to turn a class that is like this:</p>
  
  <pre><code>class Foo(object):
      BAR = 23
      def __init__(self):
          self.a = 5
  
      def pointless():
          print 'pointless.'
  
      def change_a(self):
          self.a = 52
  </code></pre>
  
  <p>Into this:</p>
  
  <pre><code>class Foo(object):
      BAR = 23
      instances = []
      all = # Some black magic to create the special "all" instance
      def __init__(self):
          self.a = 5
          Foo.instances.append(self)
  
      def pointless(self):
          print 'pointless.'
  
      def change_a(self):
          self.a = 52
  </code></pre>
  
  <p>And be able to use it like this:</p>
  
  <pre><code>&gt;&gt;&gt; Foo()
  &gt;&gt;&gt; Foo.instances[0]
  &lt;__main__.Foo instance at 0x102ff5758&gt;
  &gt;&gt;&gt; Foo()
  &gt;&gt;&gt; len(Foo.instances)
  2
  &gt;&gt;&gt; Foo.all.a = 78
  78
  &gt;&gt;&gt; Foo.all.a
  [78, 78]
  &gt;&gt;&gt; Foo.all.change_a()
  &gt;&gt;&gt; Foo.all.a
  [52, 52]
  &gt;&gt;&gt; 
  </code></pre>