Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
流畅的Python(第2版)
by
Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from
流畅的Python(第2版)
512
|
第
18
章
contextlib
包中还有下面一些函数。
closing
如果对象提供了
close()
方法,但没有实现
__enter__
/
__exit__
接口,则可以使用这个
函数构建上下文管理器。
suppress
构建临时忽略指定异常的上下文管理器。
nullcontext
Python 3.7
新增。一个什么也不做的上下文管理器,可以简化未实现合适上下文管理器
的对象周围的条件逻辑。当你不确定
with
块之前的条件代码有没有为
with
语句提供上
下文管理器时,就可以使用
nullcontext
代替。
contextlib
模块提供的几个类和一个装饰器比以上函数的使用范围更广。
@contextmanager
这个装饰器把简单的生成器函数变成上下文管理器,免得创建类去实现上下文管理器协
议。详见
18.2.2
节。
AbstractContextManager
Python 3.6
新增。确立上下文管理器接口的抽象基类。子类化该基类创建上下文管理器
类会更容易一些。
ContextDecorator
这个基类用于定义基于类的上下文管理器。这种上下文管理器也可用作函数装饰器,在
受管理的上下文中运行整个函数。
ExitStack
这个上下文管理器能进入多个上下文管理器。
with
块结束时,
ExitStack
按照后进先出
(
LIFO
)
顺序调用栈中各个上下文管理器的
__exit__
方法。如果你事先不知道
with
块
要进入多少个上下文管理器,则可以使用这个类。例如,同时打开任意一个文件列表中
的所有文件。
Python 3.7
还为
contextlib ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial
You might also like
Python高级编程(第2版)
Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Python语言及其应用(第2版)
Bill Lubanovic
Kafka权威指南(第2版)
Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty
Python贝叶斯分析(第2版)
Posts & Telecom Press, Osvaldo Martin
Publisher Resources
ISBN: 9787115612366