title: Per-class constants in Python
body: <p>Another Python code style question.</p>
  
  <p>If I have some "constant values" (quoted because there is not such thing in Python) that apply to a single class, what is preferred: module-level variables, or class variables?</p>
  
  <p>E.g. Suppose we have a class <code>Counter</code> which does something when a constant threshold value is reached, which is preferred?</p>
  
  <pre><code>class Counter(object):
      THRESHOLD = 6
      ...
  
      def increment(self):
          self.val += 1
          if self.val == Counter.THRESHOLD:
              do_something()
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>COUNTER_THRESHOLD = 6
  
  class Counter(object):
      ...
      def increment(self):
          self.val += 1
          if self.val == COUNTER_THRESHOLD:
              do_something()
  </code></pre>
  
  <p>The only thing I've managed to dig up is from the Python docs. They suggest that class variables are ideally treated as "constant":</p>
  
  <blockquote>
    <p>Class variables can be used as defaults for instance variables, but
    using mutable values there can lead to unexpected results.</p>
  </blockquote>
  
  <p><a href="https://docs.python.org/2/reference/compound_stmts.html#class-definitions" rel="noreferrer">https://docs.python.org/2/reference/compound_stmts.html#class-definitions</a></p>
  
  <p>However, this doesn't really answer the question. 
  EDIT: As @JBernardo pointed out, this is irrelevant, as it refers to mutable data types, not to mutable variable values.</p>
  
  <p>Thoughts?</p>
  
  <p>Thanks</p>