title: Static initializer in Python
body: <p>My question is similar to <a href="https://stackoverflow.com/questions/7396092/is-there-a-static-constructor-or-static-initializer-in-python">Is there a static constructor or static initializer in Python?</a>. However I still don't quite follow how to implement a static constructor as I would in Java:</p>
  
  <pre><code>public class TestBuilder {
      private String uid;
      private String name;
      private double speed;
  
      public static final TestBuilder SLEEPY;
      public static final TestBuilder SPEEDY;
  
      static {
          SLEEPY = new TestBuilder("1", "slow test", 500.00);
          SPEEDY = new TestBuilder("2", "fast test", 2000.00);
      }
  
      private TestBuilder(String uid, String name, double speed){
          this.uid = uid;
          this.name = name;
          this.speed = speed;
      }
  
      public double getSpeed(){
          return speed;
      }
  
      public String getUid() {
          return uid;
      }
  
      public String getName() {
          return name;
      }
  </code></pre>
  
  <p>In java from another class I could then call TestBuilder.SLEEPY to access a populated class.</p>
  
  <p>In python I have tried:</p>
  
  <pre><code>class TestBuilder:
      uid = str()
      name = str()
      speed = float()
  
      def __init__(self, uid, name, speed):
          self.uid = uid
          self.name = name
          self.speed = speed
  
      def lookupByName(self, name):
          result = None
          members = [attr for attr in dir(TestBuilder()) if not callable(attr) and not   attr.startswith("__")]
          for testbuilder in members:
          if testbuilder.name == name:
              result = testbuilder.uid
              break         
          return result
  
  TestBuilder.SLEEPY = TestBuilder("1","slow test", 500.0)
  TestBuilder.SPEEDY = TestBuilder("2","fast test", 2000.0)
  </code></pre>
  
  <p>Then in another module I tried:</p>
  
  <pre><code>from examples.testbuilder import TestBuilder
  import unittest
  
  class Tester(unittest.TestCase):
  
      def test_builder(self):
          dummy = TestBuilder
          ref = dummy.SPEEDY
          sleepyid = dummy.lookupByName("slow test")
          self.assertTrue(dummy.SPEEDY.__str__() == ref.__str__())
          self.assertEqual(1, sleepyid)
  </code></pre>
  
  <p>However I get a "TypeError: lookupByName() missing 1 required positional argument: 'name'" on the dummy.lookupByName("slow test") call and am not sure why.
  Does this look like a 'pythonic' way to generate similar functionality to a Java static initializer? Are there alternatives?</p>