Is there a way to hook a .decode
call into the format specification? There may be reasons i.e. a large buffer, not to decode the everything, and it can be inconvenient to call decode
on every single argument.
In [473]: print(b'hello world' + b', John')
b'hello world, John'
But:
In [475]: print('{}, {}'.format(b'hello world', b'John'))
b'hello world', b'John'
The format string is still a string literal merely with a 'b' included so:
In [477]: print('{}, {}'.format(b'hello world', b'John').encode())
b"b'hello world', b'John'"
Edit, Something like this is also possible, but blindly looping try-excepts is rather bad:
def decoder_step(s):
try: return s.decode()
except: return s
decoder = lambda x: tuple(decoder_step(s) for s in x)
In [3]: "{} {} {}".format(*decoder([b'foo', 3, b'bar', 'man']))
Out[3]: 'foo 3 bar'