[edu] context manager (__enter__, __exit__, with as, 컨텍스트 매니저)
[file access] f = open('mytextfile.txt', 'w', encoding='utf8') for x in range(2, 10): for y in range(2, 10): f.write("{:02d} x {:02d} = {:02d}".format(x, y, x*y) + '\n') f.write("\n"*2) f.close() ..
burningrizen.tistory.com
stackoverflow.com/questions/3774328/implementing-use-of-with-object-as-f-in-custom-class-in-python
Implementing use of 'with object() as f' in custom class in python
I have to open a file-like object in python (it's a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the...
stackoverflow.com
stackoverflow.com/questions/34749943/python-with-as-for-custom-context-manager
Python with...as for custom context manager
I wrote a simple context manager in Python for handling unit tests (and to try to learn context managers): class TestContext(object): test_count=1 def __init__(self): self.test_num...
stackoverflow.com
주의!! __enter__, __exit__ 만들어서 원하는 처리방식 추가하면 되나
__enter__ 에서 반드시 return self 가 되어야
with Custom_with_class() as cwc:
cwc.do_sth() # 이런식으로 접근이 가능함
Assuming that you need to access the context manager created in the with statement, __enter__needs to return self.
'시행착오 > [python]' 카테고리의 다른 글
[python] paramiko ssh 연결되어있는 상태인지 체크법 (0) | 2021.04.15 |
---|---|
[python] asyncio + multithreading: one asyncio event loop per thread (0) | 2021.04.14 |
python에서 json data다룰때 key가 존재하는지 확인하는 방법 (0) | 2021.03.31 |
파이썬 requests 비동기로 할 때 arguments 여러개 보내는 법 (0) | 2021.03.30 |
[python] logging Module - 파이썬 로그 남기기 시행착오 정리 (0) | 2021.03.23 |