- 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]
推薦閱讀
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Mastering Natural Language Processing with Python
- 小程序,巧運營:微信小程序運營招式大全
- bbPress Complete
- Kivy Cookbook
- Python大學實用教程
- Building Dynamics CRM 2015 Dashboards with Power BI
- Java 9 Programming By Example
- Three.js權威指南:在網頁上創建3D圖形和動畫的方法與實踐(原書第4版)
- Monitoring Docker
- 你好!Java
- 企業級Java現代化:寫給開發者的云原生簡明指南
- Node.js Web Development
- Getting Started with Hazelcast