Last active
June 1, 2020 08:10
-
-
Save eclecticmiraclecat/360d35f09acd2fadb5edc6dc8398886d to your computer and use it in GitHub Desktop.
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
>>> class Spam: | |
... def __init__(self, value): | |
... self.value = value | |
... def yow(self): | |
... print('Yow!') | |
... def grok(self): | |
... print('Grok!') | |
... | |
>>> vars(Spam) | |
mappingproxy({'__module__': '__main__', '__init__': <function Spam.__init__ at 0x7f95b61a0d08>, 'yow': <function Spam.yow at 0x7f95b61a0d90>, 'grok': <function Spam.grok at 0x7f95b61b0048>, '__dict__': <attribute '__dict__' of 'Spam' objects>, '__weakref__': <attribute '__weakref__' of 'Spam' objects>, '__doc__': None}) | |
>>> vars(Spam).items() | |
dict_items([('__module__', '__main__'), ('__init__', <function Spam.__init__ at 0x7f95b61a0d08>), ('yow', <function Spam.yow at 0x7f95b61a0d90>), ('grok', <function Spam.grok at 0x7f95b61b0048>), ('__dict__', <attribute '__dict__' of 'Spam' objects>), ('__weakref__', <attribute '__weakref__' of 'Spam' objects>), ('__doc__', None)]) | |
>>> | |
>>> def boo(func): | |
... def wrapper(*args, **kwargs): | |
... print('hello from inside') | |
... return func(*args, **kwargs) | |
... return wrapper | |
... | |
>>> | |
>>> | |
>>> def boo_methods(cls): | |
... for key, value in vars(cls).items(): | |
... if callable(value): | |
... setattr(cls, key, boo(value)) | |
... return cls | |
... | |
>>> | |
>>> | |
>>> @boo_methods | |
... class Spam: | |
... def __init__(self, value): | |
... self.value = value | |
... def yow(self): | |
... print('Yow!') | |
... def grok(self): | |
... print('Grok!') | |
... | |
>>> s = Spam(2) | |
hello from inside | |
>>> s.yow() | |
hello from inside | |
Yow! | |
>>> s.grok() | |
hello from inside | |
Grok! | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment