@contextmanager 這個(gè)裝飾器接受一個(gè) generator,用 yield 語(yǔ)句把 with ... as var 把變量輸出去,然后,with 語(yǔ)句就可以正常的工作了: 1 2 with create_query('Bob') as q: q.query() 很多時(shí)候,我們希望在某段代碼執(zhí)行前后自動(dòng)執(zhí)行特定代碼,也可以用 @contextmanager實(shí)現(xiàn)。 1 2 3 4 5 6 7 8 9 @conte...
www.dbjr.com.cn/article/2514...htm 2025-5-27