ncoghlan ncoghlan - 4 months ago 17
Python Question

Including a formatted iterable as part of a larger formatted string

In writing a class recently, I initially included a

method along the following lines:

return "{}({!r}, {!r}, {!r})".format(

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.

Building the format string programmatically:

fmt = "{}(%s)" % ", ".join(["{!r}"]*3)
return fmt.format(self.__class__.__name__,

Formatting the arguments separately with

args = ", ".join(map(repr, [self.arg1, self.arg2, self.arg3]))
return "{}({})".format(self.__class__.__name__, args)

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).

Inspired by Ned's answer, I've now added the following utility function to a helper module:

def format_iter(iterable, fmt='{!r}', sep=', '):
return sep.join(fmt.format(x) for x in iterable)

>>> format_iter(range(10))
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
>>> format_iter(range(10), sep='|')
>>> format_iter(range(10), fmt='{:04b}', sep='|')
>>> 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'

I ended up adding a second utility function, almost identical to the one in agf's answer:

def call_repr(name, *args):
return "{}({})".format(name, format_iter(args))

So the originally offending
function now looks like:

def __repr__(self):
return call_repr(self.__class__.__name__,

(Yes, one of the original constructor arguments went away earlier today.)

agf agf

If this is a pattern you're going to repeat, I'd probably use:

# This is a static method or module-level function
def argrepr(name, *args):
    return '{}({})'.format(name, ', '.join(repr(arg) for arg in args))

def __repr__(self):
    return argrepr(self.__name__, self.arg1, self.arg2, self.arg3)


# This is a static method or module-level function
def argrepr(*args):
    return '(' + ', '.join(repr(arg) for arg in args) + ')'

def __repr__(self):
    return repr(self.__name__) + argrepr(self.arg1, self.arg2, self.arg3)