é·ããwithæãåéããã¦ããããã
ä¹
ã
ã®æ´æ°ã
ããªãã®withæ
ã¯ã©ãããï¼ ç§ã¯open()
ããã
ãã®æç¹ã§å¯ããããã ããwithæã§çæããã¤ã³ã¹ã¿ã³ã¹ã¯ä½¿ãæ¨ã¦ã ã¨æã£ã¦ããã¨ããã ãã®è©±ã
ã§ã¯ããªãåéãããã¦ããã®ãã¨ããè¨ã訳ããã
ãã£ãã以ä¸ã®ã³ã¼ããè¦ã¦ã»ããã
# shop.txtã¯å¾ã æ¸ãã³ã¼ãã§ã®åºåçµæã io = open("shop.txt", "r", encoding="utf-8") for i in range(2): with io: for s in io: print(s.strip())
io
ã¨ããTextIOWrapper
ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦2åwithæ
ã§å
容ãåºåãããã¨ãã¦ããã ãã®ã³ã¼ãã§ããã
ããããããã ããã ãããï¼
åºåãã覧ããã ããã
--- Xæ1æ¥ --- éåº: OtsuShop å ¥åº: C(22) å ¥åº: C(29) å ¥åº: C(19) å ¥åº: A(23) éåº: A(23) éåº: C(29) éåº: C(22) éåº: C(19) éåº: OtsuShop B(27)ã¯å¶æ¥æéå¤ã®ããå ¥åºã§ãã¾ããã§ããã --- Xæ2æ¥ --- éåº: OtsuShop å ¥åº: A(27) å ¥åº: A(29) å ¥åº: C(20) å ¥åº: C(22) éåº: C(20) éåº: A(27) éåº: C(22) éåº: A(29) éåº: OtsuShop C(29)ã¯å¶æ¥æéå¤ã®ããå ¥åºã§ãã¾ããã§ããã Traceback (most recent call last): File ..., line 107, in <module> with io: ValueError: I/O operation on closed file.
2åç®ã®withæ
ã使ããã¨ããã¨ã¨ã©ã¼ã«ãªãã®ã§ããã
ããã¯TextIOWrapper
ãä¸è¦ªåãªã®ã趣å³ã§æ¸ãã¦ãç¨åº¦ã®ééã«ã¯ããããªãä»æ¹ãªãäºæ
ããã£ã¦ã®ãã¨ãªã®ãå®ãã§ã¯ãªãããwithãããã¯çµäºæã«å¼ã³åºããã__exit__
ã¡ã½ããã§ãã¡ã¤ã«ãéããå¦çï¼ã¯ãã¦ããããã®ã®__enter__
ã¡ã½ããã§ãã¡ã¤ã«ãéãå¦çï¼ã¯ãã¦ãããªãã¨ããã ãã®ãã¨ãããã
__enter__
, __exit__
ã¯èªä½ã¯ã©ã¹ã§ãå®è£
ãããã¨ã¯ãããã®ã®ãTextIOWrapper
ãããã__enter__
ã§ã¯selfãè¿ãã ãã§ãªã«ãè¡ããã__exit__
ã§çµäºå¦çãå®è£
ããã ãã®ãã®ã°ããã§ãã£ãã®ã§ãä»ã¾ã§æ°ã¥ããã¨ããªãã£ãã
ã§ã¯ãªãæ°ä»ããã®ãã¨ããã¨ãthreading
ã®æä»å¶å¾¡Lock
ã«ã¤ãã¦èª¿ã¹ã¦ããæã«with lock:
ã®ãããªã¤ã³ã¹ã¿ã³ã¹ã§withãããã¯ã«å
¥ããããªã³ã¼ããè¦ãããããã§ããã
å·éã«èããã¨ãã¤ã³ã¹ã¿ã³ã¹çæ -> __enter__å¼ã³åºã
ã¨ããæµããªã®ã§å½ããåãªã®ã ããç²ç¹ã§ãã£ãã
ããããLock
ãç¹æ®ãªã®ã§ã¯ï¼ã¨çå¿æ鬼ã«ãªã£ã¦ããã®ã§ä»¥ä¸ã®ã³ã¼ãã§è©¦ãã¦ã¿ããã¨ã«ããã
import random as rnd from typing import Iterator, Self class Human: ID = {} __name: str __age: int def __new__(cls, name: str, age: int) -> Self: data = (name, age) if data in cls.ID: return cls.ID[data] self = super().__new__(cls) self.__name = name self.__age = age cls.ID[data] = self return self def __str__(self) -> str: return f"{self.__name}({self.__age})" class Shop(list[Human]): def __init__(self, name: str) -> None: self.__name = name self.__is_open = False super().__init__() def __enter__(self) -> Self: self.open() return self def __exit__(self, *ex) -> None: self.close() def __str__(self) -> str: return f"{self.__name}" def append(self, human: Human) -> None: if human in self: return if not self.__is_open: print(f"{human}ã¯å¶æ¥æéå¤ã®ããå ¥åºã§ãã¾ããã§ããã") return print(f"å ¥åº: {human}") super().append(human) def remove(self, human: Human) -> None: if human not in self: return print(f"éåº: {human}") super().remove(human) def close(self) -> None: while self: human = rnd.choice(self) self.remove(human) self.__is_open = False print(f"éåº: {self}") def open(self) -> None: self.__is_open = True print(f"éåº: {self}") def create_human() -> Iterator[Human]: names = tuple( chr(x) for x in range( ord("A"), ord("D"), ) ) ages = tuple(range(18, 30)) while True: yield Human(rnd.choice(names), rnd.choice(ages)) def main() -> None: shop = Shop("OtsuShop") gen_customer = iter(create_human()) for day in range(2): print(f"--- Xæ{day+1}æ¥ ---") with shop: while len(shop) < 4: shop.append(next(gen_customer)) shop.append(next(gen_customer)) print() if __name__ == "__main__": main()
Shop
ã¯ã©ã¹ã¯éåº
, éåº
ã¨ããç¶æ
ãæã¡ãéåºä¸ã¯append
ã§ããªããªã¹ãã®æ¡å¼µã¯ã©ã¹ã
__enter__
å¼ã³åºãã§éåºãã__exit__
å¼ã³åºãã§ä»ãã客ãå
¨å¡éåºããéåºããã
ãã¨ã¯å
ã»ã©ã¨åãããã«with shop:
ãï¼åçºçããããã«foræãåãã¦ããã
withãããã¯ä¸ã«shopã«å®¢ã4人å
¥ããããã«withãããã¯å¤ã§shopã«å®¢ã1人å
¥ãã¦ããã
--- Xæ1æ¥ --- éåº: OtsuShop å ¥åº: C(22) å ¥åº: C(29) å ¥åº: C(19) å ¥åº: A(23) éåº: A(23) éåº: C(29) éåº: C(22) éåº: C(19) éåº: OtsuShop B(27)ã¯å¶æ¥æéå¤ã®ããå ¥åºã§ãã¾ããã§ããã --- Xæ2æ¥ --- éåº: OtsuShop å ¥åº: A(27) å ¥åº: A(29) å ¥åº: C(20) å ¥åº: C(22) éåº: C(20) éåº: A(27) éåº: C(22) éåº: A(29) éåº: OtsuShop C(29)ã¯å¶æ¥æéå¤ã®ããå ¥åºã§ãã¾ããã§ããã
withãããã¯å
¥ãã§éåº: OtsuShop
ãåºåããã4人å
¥åºããæç¹ã§withãããã¯ãæããã¨éåºãã°ãç¶ããå¾éåº: OtsuShop
ã¨åºåãããã
ãã®å¾å
¥åºãããã¨ãã客ã«å¯¾ãã¦ã¯å
¥åºãã§ãã¦ããªããã¨ãåããã
ããããããä¸åº¦withãããã¯ã«å
¥ãã¨åã³å®¢ãåãå
¥ãããã¨ãã§ããããã«ãªã£ã¦ããã
ãã®ããã«è¤æ°åwithæã使ãããã¯ã¯ã©ã¹ã®å®è£ 次第ã§ããã¨ãããã¨ãç´å¾ã§ããã