I didn't have to write a decorator in the class Thank you contextmanager

Previous post Yotsubato! Amazingly cool I wish I had used contextmanager from the beginning without the difficulty of decorating ...

yo.py


from contextlib import contextmanager
import random

class Prot:
    def __init__(self):
        pass

    @contextmanager
    def yoyo(self, hey):
        print('gacha.') #  __enter__
        yield hey # as
        print('Everything Done Right.') #  __exit__

    def yo_gacha(self, y):
        yo = [y*i for i in range(10)] 
        return yo[random.randint(0, 9)]    

if __name__ == '__main__':
    p = Prot()
    with p.yoyo('hey') as y:
        print(p.yo_gacha(y))

contextlib.contextmanager is a decorator that applies with to a good feeling and divides it into __enter__ and __exit__ with yield in between. One indent is sacrificed, but the description is easy and very easy to understand It is easy to understand that if you pass it with yield, you can receive it with ʻas`. It may be better to use a decorator when I want to wrap various things, but I learned

Recommended Posts

I didn't have to write a decorator in the class Thank you contextmanager
I didn't want to write the AWS key in the program
Write decorator in class
I want to write in Python! (2) Let's write a test
I want to write in Python! (3) Utilize the mock
How to use the __call__ method in a Python class
To write a test in Go, first design the interface
I made a class to get the analysis result by MeCab in ndarray with python
How to write a Python class
I wrote the code to write the code of Brainf * ck in python
I made a command to display a colorful calendar in the terminal
Write a script in Shell and Python to notify you in Slack when the process is finished
Have Alexa run Python to give you a sense of the future
Write the test in a python docstring
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
Various comments to write in the program
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
Voice actor history Scraping Wikipedia to see how many years have you become a voice actor in the "Aikatsu!" Series
Feel free to write a test with nose (in the case of + gevent)
I tried to create a class that can easily serialize Json in Python
[Python] Created a class to play sin waves in the background with pyaudio
If you write the View decorator in urls.py in Django, the list will be higher.
Test the number of times you have thrown a query (sql) in django
I made an appdo command to execute a command in the context of the app
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I searched for the skills needed to become a web engineer in Python
I want to create a window in Python
I wrote a class in Python3 and Java
The trick to write flatten concisely in python
Why does Python have to write a colon?
How to write a named tuple document in 2020
I want to write to a file with Python
I want to display the progress in Python!
I did a little research on the class
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
I can't find the clocksource tsc! ?? The story of trying to write a kernel patch
I want to write a triple loop and conditional branch in one line in python
I tried to create a class to search files with Python's Glob method in VBA
I want to write in Python! (1) Code format check
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
How to write a GUI using the maya command
I want to transition with a button in flask
I made a script to put a snippet in README.md
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I want to work with a robot in python.
Write a log-scale histogram on the x-axis in python
I made a command to markdown the table clipboard
When you want to plt.save in a for statement
Do not pass self to ProcessPoolExecutor in the class
I want to use the R dataset in python
I wrote you to watch the signal with Go
Make a note of what you want to do in the future with Raspberry Pi
[Small story] A painstaking measure when you have to execute a function before import in Python
I have read 10 books related to time series data, so I will write a book review.
I want to see the graph in 3D! I can make such a dream come true.