Created
July 8, 2012 04:13
-
-
Save tkaemming/3069262 to your computer and use it in GitHub Desktop.
__repr__ with added attributes
This file contains hidden or 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 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