Why wrap a single static method in a class?
<p>I'm reading some code where the author is using a coding style with which I am unfamiliar; they put absolutely every function definition into a class. For example (details removed so as to not identify the author and codebase):</p>

<pre><code>class CSVChecker:
    @staticmethod
    def is_ok(file):
        #some stuff that could return False
        return True
</code></pre>

<p>and that's the end of this class. Many similar. No <code>__init__</code> or <code>self</code>. Usage: <code>if CSVChecker.is_ok(afile)</code></p>

<p>Is this just an odd stylistic quirk carried over to Python from some other language? Or is there a Pythonic reason for this, rather than just <code>def csv_check_file_ok(file):</code> at the top level of the file?</p>

<p>@deceze probably has the answer in his comment. Some of these function-objects are indeed stored in lists, and some elements of the lists may be instances of "proper" objects. Stripped down to the absolute basics:</p>

<pre><code>class F1:
  def __init__(self, a):
    self.max=a
  def ok(self, x): 
    return x &lt; self.max

class F2:
  @staticmethod
  def ok(x):
     return x &gt; 0
</code></pre>

<p>elsewhere</p>

<pre><code>checkers = []
...
checkers.append( F1(i+j) )
checkers.append( F2  )
....
if ( all( check.ok(x) for check in checkers )
</code></pre>

<p>Personally I'd not have bothered with <code>@staticmethod</code> and just written the classes that didn't need any initialization with a dummy <code>def __init__(self): pass</code> and instantiate <code>F2()</code> Also there's probably some carry-over of style from another language (Java?) because not all the single-static-method function objects are used in this way. There again, if the author thought that they might be so used in the future, or might acquire the need to be instantiated with parameters, it makes sense.</p>

<p>Anyway, I've learned something and hopefully others will do in future. </p>

<p>EDIT added later: this usage accomplishes the same as what might have been done using <code>functools.partial</code> </p>

<pre><code>def f1( x, max=None ):
   return x &lt; max
#elsewhere ...
checkers.append( functools.partial( f1, max=i+j ))
</code></pre>

<p>Now pondering which is best. Also how this usage of classes fits into the "inheritance, composition, aggregation" classification of objects. And whether it's an exception to</p>

<p><em>There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.</em></p>

python oop