Skip to content

Instantly share code, notes, and snippets.

@satishkt
Created May 3, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishkt/232e870c7e440a1aa984948949fe08a7 to your computer and use it in GitHub Desktop.
Save satishkt/232e870c7e440a1aa984948949fe08a7 to your computer and use it in GitHub Desktop.
Tuple prints as object address when print. This prints the attributrs.
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