Tagged articles
with statement
18 articles · Page 1 of 1
The article explains how Python’s context manager (the with statement) provides an elegant, exception‑safe way to acquire and release resources such as files, database connections, locks, and even asynchronous handles, showing concrete code examples, custom implementations, and best‑practice guidelines.
Best PracticesPythonasync
0 likes · 12 min read

This tutorial explains why traditional try‑finally resource handling is cumbersome, introduces Python’s with statement and the underlying __enter__/__exit__ magic methods, provides practical examples such as timers, database connections, temporary directories, and shows how to simplify custom managers with contextlib while covering best practices and performance considerations.
Exception HandlingPythoncontext manager
0 likes · 10 min read

This article explains how to use Python's try‑except and with statements to safely read and process CSV and JSON files, covering common errors such as missing files, permission issues, format problems, and providing best‑practice examples for robust error handling.
CSVJSONexception-handling
0 likes · 7 min read

This article explains the concept of Python context managers, demonstrates how the with statement simplifies resource handling compared to manual open/close patterns, and shows how defining __enter__ and __exit__ methods enables cleaner code, testing, and error handling.
Pythoncontext managerpythonic
0 likes · 5 min read

This article explains Python's with statement as a context management tool, describes the required __enter__ and __exit__ methods, and provides multiple practical examples—including file handling, custom managers, multiple managers, third‑party resources, and automation scenarios—to illustrate reliable resource acquisition and cleanup.
context managerresource managementwith statement
0 likes · 8 min read

This article explains Python's context manager mechanism, detailing how the with statement works, its __enter__ and __exit__ methods, and provides ten practical code examples ranging from file handling to network connections, database access, threading, and more.
Pythonresource managementwith statement
0 likes · 5 min read

This article explains Python's context manager protocol, detailing the roles of __enter__() and __exit__() methods, their typical use cases, and provides ten concrete code examples ranging from file handling and database connections to custom managers for networking, temporary files, and exception handling.
code-examplescontext managerresource management
0 likes · 5 min read

This article explains the purpose and usage of Python's with statement, demonstrates how it simplifies resource management compared to manual try‑finally blocks, and shows how to implement custom context managers using magic methods or the contextlib decorator.
Exception HandlingPythoncontext manager
0 likes · 6 min read

This article explains the purpose and mechanics of Python's with statement, showing how it leverages context managers to replace verbose try‑finally blocks, and demonstrates both class‑based and decorator‑based implementations with clear code examples.
Code ExampleDecoratorPython
0 likes · 6 min read

This article explains how the JavaScript with statement creates a new lexical scope that can unintentionally create global variables, cause data leakage, and dramatically degrade performance, while also covering LHS/RHS lookup, execution contexts, variable/activation objects, and scope chain mechanics.
Execution ContextJavaScriptLHS
0 likes · 8 min read

JavaScript, despite its evolution and flexibility, still retains three outdated features—void operator, with statement, and labels—that remain functional but are inefficient or confusing, and developers are advised to avoid using them in modern code to maintain readability and prevent bugs.
Best PracticesJavaScriptLabels
0 likes · 9 min read

This article translates and explains PEP 343, which introduces the ‘with’ statement and the context‑manager protocol (__enter__ and __exit__) in Python, detailing its motivation, design decisions, syntax, example implementations, and its impact on resource handling and code clarity.
PEPcontext managergenerator
0 likes · 25 min read

This article explains Python's file handling basics, covering what files are, how to open them with the open() function, various access modes, common file object methods such as read, readline, readlines, write, tell, seek, and the use of the with statement for safe resource management, plus code examples.
File I/OPythonfile handling
0 likes · 11 min read

This article explains Python's context manager mechanism, covering the basic with statement, custom __enter__/__exit__ classes, the contextlib.contextmanager decorator, nesting, combining multiple managers with ExitStack, and practical applications such as SQLAlchemy session handling, exception management, and persistent HTTP requests.
DecoratorPythonSQLAlchemy
0 likes · 6 min read

This article explores the subtle bugs that arise when using Python's with‑statement for a global process lock, explains why exceptions raised in __enter__ are not caught by __exit__, and presents three practical solutions using contextlib utilities and simple control flow.
context managercontextlibglobal lock
0 likes · 6 min read

The article examines a flawed implementation of a global process lock using Python's with statement, explains why exceptions raised in __enter__ are not caught by __exit__, demonstrates failing unit tests, and presents three alternative solutions—including a custom ABContext, use of the deprecated contextlib.nested, and a simple if‑statement workaround—to correctly manage exceptions in context managers.
Exception HandlingPythoncontext manager
0 likes · 6 min read

This comprehensive guide walks you through Python's exception classes, various try‑except‑else‑finally patterns, handy shortcuts like assert and with, how to raise custom exceptions, and retrieving exception details via the sys module, all illustrated with clear examples and diagrams.
Exception HandlingPythonassert
0 likes · 9 min read


21CTOFeb 16, 2016 · Frontend Development
This article explains the JavaScript with statement, its syntax and intended convenience, then details why it is discouraged by highlighting severe performance degradation, ambiguous scope resolution, debugging difficulties, and potential bugs illustrated through multiple code examples and analysis.
DebuggingJavaScriptcode optimization
0 likes · 9 min read
