How do most people do console.log debugging in Python?
<p>In Node.js when I want to quickly check the value of something rather than busting out the debugger and stepping through, I quickly add a console.log(foo) and get a beautiful:</p>

<pre><code>{
   lemmons: "pie",
   number: 9,
   fetch: function(){..}
   elements: {
      fire: 99.9
   }
}
</code></pre>

<p>Very clear! In Python I get this:</p>

<pre><code>class LinkedList:
  head = None
  tail = None
  lemmons = 99
</code></pre>

<p><code>&lt;__main__.LinkedList instance at 0x105989f80&gt;</code>
or with <code>vars()</code>,</p>

<p><code>{}</code></p>

<p>or with <code>dir()</code>, </p>

<p><code>['_LinkedList__Node', '__doc__', '__module__', 'append', 'get_tail', 'head', 'lemmons', 'remove', 'tail']</code></p>

<p>Yuck! Look at all that nonsense - I thought python was supposed to be fast, beautiful and clean? Is this really how people do it? Do they implement customer <strong>str</strong> and custom <strong>repr</strong> for everything? Because that seems kind of crazy too..</p>

<p>The expectation is that you <a href="https://docs.python.org/2/howto/logging.html#using-arbitrary-objects-as-messages" rel="nofollow">implement your own <code>__str__</code> method</a> so that you can pick what is important to log in your diags.</p>

<p>However, there is the option to log the whole object dictionary in a "pretty" format using a couple of lines of code.  For example:</p>

<pre><code>from pprint import pformat

class A(object):
    def __init__(self):
        self.foo = 1
        self.bar = {"hello": "world"}
        self._private = 0

a = A()
print pformat(vars(a))
# You can also pass pformat to your logger if that's what you have.
</code></pre>

<p>This will return something like this (depending on how much data you have and thw <code>width</code> constraint):</p>

<pre><code>{'_private': 0,
 'bar': {'hello': 'world'},
 'foo': 1}
</code></pre>

python class debugging object tostring