- Mastering Concurrency in Python
- Quan Nguyen
- 130字
- 2021-06-10 19:24:05
The syntax of the with statement
The syntax of the with statement can be intuitive and straightforward. With the purpose of wrapping the execution of a block with methods defined by a context manager, it consists of the following simple form:
with [expression] (as [target]):
[code]
Note that the as [target] part of the with statement is actually not required, as we will see later on. Additionally, the with statement can also handle more than one item on the same line. Specifically, the context managers created are treated as if multiple with statements were nested inside one another. For example, look at the following code:
with [expression1] as [target1], [expression2] as [target2]:
[code]
This is interpreted as follows:
with [expression1] as [target1]:
with [expression2] as [target2]:
[code]
推薦閱讀
- Node.js 10實戰
- Objective-C Memory Management Essentials
- 深入理解Django:框架內幕與實現原理
- Mastering Selenium WebDriver
- 劍指JVM:虛擬機實踐與性能調優
- MySQL 8 DBA基礎教程
- HTML5 Mobile Development Cookbook
- 前端架構:從入門到微前端
- Spring Cloud、Nginx高并發核心編程
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- 持續輕量級Java EE開發:編寫可測試的代碼
- UML2面向對象分析與設計(第2版)
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- Visual FoxPro程序設計習題及實驗指導
- Tkinter GUI Programming by Example