title: Python - Why does __str__ want something returned when it called? I have print functions inside of __str__
body: <p>I am building a simple class in python. I have defined my own <code>__str__</code> method that should work nicely when I call print on an instance of the class. When I create an instance of the class and call <code>print</code> on it I get an error:</p>
  
  <pre><code>TypeError: __str__ returned non-string (type NoneType)
  </code></pre>
  
  <p>I understand this error, it is telling me the function did not return anything (it returned <code>None</code>)</p>
  
  <pre><code>class Car(object):
  
      def __init__(self, typ, make, model, color, year, miles):
          self.typ = typ
          self.make = make
          self.model = model
          self.color = color.lower()
          self.year = year
          self.miles = miles
  
      def __str__(self):
          print('Vehicle Type: ' + str(self.typ))
          print('Make: ' + str(self.make))
          print('Model: ' + str(self.model))
          print('Year: ' + str(self.year))
          print('Miles: ' + str(self.miles))
          #return ''  # I can avoid getting an error if I un-comment this line
  
  bmw = Car('SUV', 'BMW', 'X5', 'silver', 2003, 12030)
  print bmw
  </code></pre>
  
  <p>As you can see my <code>__str__</code> function has all the print statements that I want. I have no need for it to return anything. This is the output I want.</p>
  
  <pre><code>Vehicle Type: SUV
  Make: BMW
  Model: X5
  Year: 2003
  Miles: 12030
  </code></pre>
  
  <p>How can I get this output?
  I have tried this to avoid getting an error printed but the erro still comes up:</p>
  
  <pre><code>def __str__(self):
      try:
          print('Vehicle Type: ' + str(self.typ))
          print('Make: ' + str(self.make))
          print('Model: ' + str(self.model))
          print('Year: ' + str(self.year))
          print('Miles: ' + str(self.miles))
      except:
          pass
  </code></pre>