title: Module function vs staticmethod vs classmethod vs no decorators: Which idiom is more pythonic?
body: <p>I'm a Java developer who's toyed around with Python on and off. I recently stumbled upon <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="noreferrer">this article</a> which mentions common mistakes Java programmers make when they pick up Python. The first one caught my eye:</p>
  
  <blockquote>
    <p>A static method in Java does not translate to a Python classmethod. Oh sure, it results in more or less the same effect, but the goal of a classmethod is actually to do something that's usually not even possible in Java (like inheriting a non-default constructor). The idiomatic translation of a Java static method is usually a module-level function, not a classmethod or staticmethod. (And static final fields should translate to module-level constants.)</p>
    
    <p>This isn't much of a performance issue, but a Python programmer who has to work with Java-idiom code like this will be rather irritated by typing Foo.Foo.someMethod when it should just be Foo.someFunction. But do note that calling a classmethod involves an additional memory allocation that calling a staticmethod or function does not.</p>
    
    <p>Oh, and all those Foo.Bar.Baz attribute chains don't come for free, either. In Java, those dotted names are looked up by the compiler, so at runtime it really doesn't matter how many of them you have. In Python, the lookups occur at runtime, so each dot counts. (Remember that in Python, "Flat is better than nested", although it's more related to "Readability counts" and "Simple is better than complex," than to being about performance.)</p>
  </blockquote>
  
  <p>I found this a bit strange because the documentation for <a href="http://docs.python.org/py3k/library/functions.html#staticmethod" rel="noreferrer">staticmethod</a> says:</p>
  
  <blockquote>
    <p>Static methods in Python are similar to those found in Java or C++. Also see classmethod() for a variant that is useful for creating alternate class constructors.</p>
  </blockquote>
  
  <p>Even more puzzling is that this code:</p>
  
  <pre><code>class A:
      def foo(x):
          print(x)
  A.foo(5)
  </code></pre>
  
  <p>Fails as expected in Python 2.7.3 but works fine in 3.2.3 (although you can't call the method on an instance of A, only on the class.)</p>
  
  <p>So there's three ways to implement static methods (four if you count using classmethod), each with subtle differences, one of them seemingly undocumented. This seems at odds with Python's mantra of <i>There should be one-- and preferably only one --obvious way to do it.</i> Which idiom is the most Pythonic? What are the pros and cons of each?</p>
  
  <p>Here's what I understand so far:</p>
  
  <p><b>Module function:</b></p>
  
  <ul>
  <li>Avoids the Foo.Foo.f() problem</li>
  <li>Pollutes the module's namespace more than the alternatives</li>
  <li>No inheritance</li>
  </ul>
  
  <p><b>staticmethod:</b></p>
  
  <ul>
  <li>Keeps functions related to the class inside the class and out of the module namespace.</li>
  <li>Allows calling the function on instances of the class.</li>
  <li>Subclasses can override the method.</li>
  </ul>
  
  <p><b>classmethod:</b></p>
  
  <ul>
  <li>Identical to staticmethod, but also passes the class as the first argument.</li>
  </ul>
  
  <p><b>Regular method (Python 3 only)</b>:</p>
  
  <ul>
  <li>Identical to staticmethod, but can't call the method on instances of the class.</li>
  </ul>
  
  <p>Am I overthinking this? Is this a non-issue? Please help!</p>