Skip to content

Instantly share code, notes, and snippets.

@draperjames
Created March 9, 2018 01:10
Show Gist options
  • Save draperjames/012e04f3f259c6bd01fc478aca14fd9f to your computer and use it in GitHub Desktop.
Save draperjames/012e04f3f259c6bd01fc478aca14fd9f to your computer and use it in GitHub Desktop.
A wrapper for __repr__s
def __repr__wrapper(self):
"""Show all attributes."""
return "Attributes: "+", ".join(list(self.__dict__.keys()))
def __repr__dec(func):
"""Replaces the __repr__ function of a class with __repr__wrapper"""
def call(*args, **kwargs):
func.__repr__ = __repr__wrapper
result = func(*args, **kwargs)
return result
return call
@__repr__dec
class Container(object):
"""Empty class.
"""
def __init__(self, *args, **kwargs):
self.metadata = args[0]
for k,v in kwargs.items():
self.__dict__[k] = v
occ = Container(42, how="now")
if __name__ == "__main__":
print(occ)
print(occ.metadata)
print(occ.how)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment