Created
March 9, 2018 01:10
-
-
Save draperjames/012e04f3f259c6bd01fc478aca14fd9f to your computer and use it in GitHub Desktop.
A wrapper for __repr__s
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 __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