title: Boolean value of objects in Python
body: <p>As we know, Python has boolean values for objects: If a class has a <code>__len__</code> method, every instance of it for which <code>__len__()</code> happens to return 0 will be evaluated as a boolean <code>False</code> (for example, the empty list).</p>
  <p>In fact, every iterable, empty custom object is evaluated as <code>False</code> if it appears in boolean expression.</p>
  <p>Now suppose I have a class <code>foo</code> with attribute <code>bar</code>. How can I define its truth value, so that, say, it will be evaluated to <code>True if bar % 2 == 0</code> and <code>False</code> otherwise?</p>
  <p>For example:</p>
  <pre><code>myfoo = foo()
  myfoo.bar = 3
  def a(foo):
      if foo:
          print &quot;spam&quot;
      else:
          print &quot;eggs&quot;
  </code></pre>
  <p>so, <code>a(myfoo)</code> should print <code>&quot;eggs&quot;</code>.</p>