Reputation: 41486
In writing a class recently, I initially included a __repr__
method along the following lines:
return "{}({!r}, {!r}, {!r})".format(
self.__class__.__name__,
self.arg1,
self.arg2,
self.arg3)
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__,
self.arg1,
self.arg2,
self.arg3)
Formatting the arguments separately with str.join
:
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).
Update: 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='|')
'0|1|2|3|4|5|6|7|8|9'
>>> format_iter(range(10), fmt='{:04b}', sep='|')
'0000|0001|0010|0011|0100|0101|0110|0111|1000|1001'
>>> 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'
Update2: 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 __repr__
function now looks like:
def __repr__(self):
return call_repr(self.__class__.__name__,
self.arg1,
self.arg2)
(Yes, one of the original constructor arguments went away earlier today.)
Upvotes: 13
Views: 2655
Reputation: 176760
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)
or
# 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)
Upvotes: 9
Reputation: 19759
I might do it like:
def __repr__(self):
return "{0}({1})".format(
type(self).__name__,
", ".join(repr(arg) for arg in (self.arg1, self.arg2, self.arg3)))
or:
_init_getter = operator.attrgetter('arg1', 'arg2', 'arg3')
def __repr__(self):
return "{0}({1})".format(
type(self).__name__,
", ".join(repr(arg) for arg in self._init_getter(self)))
Upvotes: 0
Reputation: 375554
My inclination would be, if you don't like the code, hide it in a function:
def repr_all(*args):
return ", ".join(repr(a) for a in args)
def __repr__(self):
args = repr_all(self.arg1, self.arg2, self.arg3)
return "{}({})".format(self.__class__.__name__, args)
Upvotes: 8