A decorator that is easy to understand but difficult to understand
I cried a little when I wrote it in the class thinking that it was already perfect
It would be nice to pass * args
and ʻargs [0]`
yo.py
import functools
import random
class Prot:
def __init__(self):
self.yo = ['yo'*i for i in range(10)]
def yoyo(f):
functools.wraps(f)
def wraps(*args):
print('yo gacha.')
result = f(args[0]) # args[0] = self, result = return value
print('')
return result
return wraps
@yoyo
def yo_gacha(self):
print(self.yo[random.randint(0, 9)])
if __name__ == '__main__':
p = Prot()
for _ in range(2): p.yo_gacha()
~ %python -m yo
yo gacha.
yoyoyoyo
yo gacha.
yoyoyoyoyoyoyoyoyo
~ %
Recommended Posts