title: Is it beneficial to replace a simple Python class with a closure?
body: <p>I have the following Python class:</p>
  
  <pre><code>class class A:
      """a class that increments internal variable"""
      def __init__(self, x):
          self._x = x
      def incr(self):
          self._x = (self._x + 1) % 10
          return self._x
  </code></pre>
  
  <p>I heard a talk that recommended that such classes with just a constructor and another method really should just be replaced by a function. </p>
  
  <p>So here is my attempt (for Python 2.7):</p>
  
  <pre><code>def incrX(x): 
      """closure that increments internal variable"""
      d = {'x' : x}
      def incr():
          d['x'] = (d['x'] + 1) % 10
          return d['x'] 
      return incr
  </code></pre>
  
  <p>Running it:</p>
  
  <pre><code>def test1():
      """testing closure vs. class"""
      print 'class...'
      a = A(10)
      print a.incr()
      print a.incr()
  
      print 'closure...'
      incr = incrX(10)
      print incr()
      print incr()
  
  $ python closure.py 
  running closure experiments
  class...
  1
  2
  closure...
  1
  2
  </code></pre>
  
  <p>So my question is:</p>
  
  <p>Is there a benefit to replacing a class like A with a closure? Just trying to understand closures better.</p>