title: How to properly implement both __str__ and __repr__
body: <p>In several of my classes, I want to implement both <code>__str__</code> and <code>__repr__</code> and usually end up with code like this:</p>
  
  <pre><code>class MyClass(object):
      def __init__(self, a):
          self.a = a
  
      def __str__(self):
          return 'MyClass({})'.format(self.a)
  
      def __repr__(self):
          return 'MyClass({!r})'.format(self.a)
  </code></pre>
  
  <p>Which does what I'd expect:</p>
  
  <pre><code>&gt;&gt;&gt; myobject = MyClass(np.array([1, 2]))
  &gt;&gt;&gt; str(myobject)
  'MyClass([1 2])'
  &gt;&gt;&gt; repr(myobject)
  'MyClass(array([1, 2]))'
  </code></pre>
  
  <p>However the code violates DRY and as the number of arguments starts to grow maintaining this becomes cumbersome and I've often found that either of <code>__str__</code> or <code>__repr__</code> has come "out of sync" with the other.</p>
  
  <p>Is there a better way to simultaneously implement both <code>__str__</code> and <code>__repr__</code> without duplication?</p>