title: why python file-write doesn't implicitly call class __str__?
body: <p>Why doesn't Python's file write automatically call <code>__str__</code>?</p>
  <pre><code>$ cat person.py
  class Person:
      def __init__(self):
          self.age = 22
      def __str__(self):
          return &quot;my age is {}&quot;.format(self.age)
  </code></pre>
  <p>When I try to print it, everything goes fine, but writing <code>Person</code> to file fails:</p>
  <pre><code>&gt;&gt;&gt; from person import Person
  &gt;&gt;&gt; dan = Person()
  &gt;&gt;&gt; print(dan)
  my age is 22
  &gt;&gt;&gt; fl = open(&quot;dan.txt&quot;,&quot;wt&quot;)
  &gt;&gt;&gt; fl.write(dan)
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  TypeError: write() argument must be str, not Person
  </code></pre>