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.)
The __format__ method is responsible for interpreting the format specifier, formatting the value, and returning the resulting string. It is safe to call this function with a value of “None” (because the “None” value in Python is an object and can have methods.)
format() formatting operations; it only works in old-style % string formatting. It indeed converts the object to a representation through the repr() function. In str. format() , ! r is the equivalent, but this also means that you can now use all the format codes for a string.
Definition and Usage The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With