Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created July 8, 2012 04:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkaemming/3069262 to your computer and use it in GitHub Desktop.
Save tkaemming/3069262 to your computer and use it in GitHub Desktop.
__repr__ with added attributes
def attribute_repr(*attributes):
"""
Example Usage:
>>> class Foo(object):
... __repr__ = attribute_repr('pk', 'slug')
<foo.models.Foo at 0x100614c50: pk=1, slug=foo>
"""
def _repr(self):
pairs = ('%s=%s' % (attribute, repr(getattr(self, attribute, None))) for attribute in attributes)
return u'<%s.%s at 0x%x: %s>' % (self.__class__.__module__, self.__class__.__name__,
id(self), ', '.join(pairs))
return _repr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment