title: Including a formatted iterable as part of a larger formatted string
body: <p>In writing a class recently, I initially included a <code>__repr__</code> method along the following lines:</p>
  
  <pre><code>return "{}({!r}, {!r}, {!r})".format(
                  self.__class__.__name__,
                  self.arg1,
                  self.arg2,
                  self.arg3)
  </code></pre>
  
  <p>Repeating the '{!r}' snippet like that feels wrong and would be tedious to maintain if I ever add any more arguments to this class. However, the more robust alternatives that occurred to me aren't going to win any prizes for elegance either.</p>
  
  <p>Building the format string programmatically:</p>
  
  <pre><code>fmt = "{}(%s)" % ", ".join(["{!r}"]*3)
  return fmt.format(self.__class__.__name__,
                    self.arg1,
                    self.arg2,
                    self.arg3)
  </code></pre>
  
  <p>Formatting the arguments separately with <code>str.join</code>:</p>
  
  <pre><code>args = ", ".join(map(repr, [self.arg1, self.arg2, self.arg3]))
  return "{}({})".format(self.__class__.__name__, args)
  </code></pre>
  
  <p>I've currently implemented the class using the last example, but I'm interested in suggestions for alternative approaches (since I'm not particularly happy with any of the above options).</p>
  
  <p><strong>Update:</strong>
  Inspired by Ned's answer, I've now added the following utility function to a helper module:</p>
  
  <pre><code>def format_iter(iterable, fmt='{!r}', sep=', '):
      return sep.join(fmt.format(x) for x in iterable)
  
  &gt;&gt;&gt; format_iter(range(10))
  '0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
  &gt;&gt;&gt; format_iter(range(10), sep='|')
  '0|1|2|3|4|5|6|7|8|9'
  &gt;&gt;&gt; format_iter(range(10), fmt='{:04b}', sep='|')
  '0000|0001|0010|0011|0100|0101|0110|0111|1000|1001'
  &gt;&gt;&gt; format_iter(range(10), fmt='{0.real}+{0.imag}j')
  '0+0j, 1+0j, 2+0j, 3+0j, 4+0j, 5+0j, 6+0j, 7+0j, 8+0j, 9+0j'
  </code></pre>
  
  <p><strong>Update2:</strong>
  I ended up adding a second utility function, almost identical to the one in agf's answer:</p>
  
  <pre><code>def call_repr(name, *args):
      return "{}({})".format(name, format_iter(args))
  </code></pre>
  
  <p>So the originally offending <code>__repr__</code> function now looks like:</p>
  
  <pre><code>def __repr__(self):
      return call_repr(self.__class__.__name__,
                       self.arg1,
                       self.arg2)
  </code></pre>
  
  <p>(Yes, one of the original constructor arguments went away earlier today.)</p>