FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/syntax/with.py at main · thomasWeise/programmingWithPythonCode · GitHub
thomasWeise
programmingWithPythonCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
syntax
/
with.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
23 lines (19 loc) · 778 Bytes
Breadcrumbs
programmingWithPythonCode
/
syntax
/
with.py
Copy path
File metadata and controls
23 lines (19 loc) · 778 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""The syntax of with-blocks in Python."""
# The 'expression' is some expression that usually acquires a resource
# which must eventually be released.
# The 'with' block does this automatically, even if an uncaught
# exception is raised within it.
# It is basically a fancy try-finally block.
with
expression
as
variable
:
# 'variable' here usually stores the acquired resource.
# It could be a handle to a file opened for writing, for example.
statement
1
# block of code that works with variable.
statement
2
# ...
# or
with
expression
:
# Here we do not explicitly work with the resource that was
# acquired. This makes sense, for example, if the resource is a lock
# to a critical section.
statement
3
# block of code
statement
4
Back
|
FazBrowse Home
|
New Git URL