Created
May 3, 2019 19:39
-
-
Save satishkt/232e870c7e440a1aa984948949fe08a7 to your computer and use it in GitHub Desktop.
Tuple prints as object address when print. This prints the attributrs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def namedtuple_to_str(t, field_widths=15): | |
if isinstance(field_widths, int): | |
field_widths = [field_widths] * len(t._fields) | |
field_pairs = ['{}={}'.format(field, getattr(t, field)) for field in t._fields] | |
s = ' '.join('{{:{}}}'.format(w).format(f) for w,f in zip(field_widths, field_pairs)) | |
result = '{}( {} )'.format(type(t).__name__, s) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment