Created
March 22, 2017 20:40
-
-
Save inirudebwoy/10d66d31c5c663942a3e826bef821504 to your computer and use it in GitHub Desktop.
Metaclass wrapping each function call with an argument printer
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
from functools import wraps | |
def printer(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
print(args) | |
return f(*args, **kwargs) | |
return wrapper | |
class MyMeta(type): | |
def __new__(meta, name, bases, dct): | |
for k, v in dct.iteritems(): | |
if callable(v): | |
v = printer(v) | |
dct[k] = v | |
return super(MyMeta, meta).__new__(meta, name, bases, dct) | |
class Foo(object): | |
__metaclass__ = MyMeta | |
def double(self, x): | |
return x * 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment