Write decorator in class

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

~ %

to be continued

=> to be continued...

Recommended Posts

Write decorator in class
Write Pulumi in Go
case class in python
Write DCGAN in Keras
Write Python in MySQL
Decorator pattern in Java
Class notation in Python
I didn't have to write a decorator in the class Thank you contextmanager
Write standard input in code
Write beta distribution in Python
Write python in Rstudio (reticulate)
Write Spigot in VS Code
Write data in HDF format
Write Spider tests in Scrapy
Get the class name where the method is defined in the decorator
Write a binary search in Python
How to write a Python class
Write a table-driven test in C
Decorator 1
Write JSON Schema in Python DSL
How to write soberly in pandas
Landmines hidden in Python class variables
Realize interface class in C language
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Write A * (A-star) algorithm in Python
[Maya] Write custom nodes in Open Maya 2.0
Read PNG chunks in Python (class)
class
Write foreign key constraints in Django
Write selenium test code in python
Decorator 2
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Examine the object's class in python
Write tests in GO language + gin
class
Write C unit tests in Python
Decorator
Decorator
Write documentation in Sphinx with Python Livereload
Generate a first class collection in Python
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
Illustrate class dependencies in an Xcode project
Implement __eq__ etc. generically in Python class
Write the test in a python docstring
Bind to class to read and write YAML
Implement a custom View Decorator in Pyramid
Decorator to avoid UnicodeEncodeError in Python 3 print ()
Write a short property definition in Python
Playing card class in Python (with comparison)
Learn the design pattern "Decorator" in Python
Write O_SYNC file in C and Python
Write a Caesar cipher program in Python
Write and execute SQL directly in Elixir
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Ajax in Django (using generic class view)
How to use __slots__ in Python class