From 0d8be77ee04ee6104d2c0492749af6b8417e9175 Mon Sep 17 00:00:00 2001 From: vansh2408 Date: Thu, 16 Oct 2025 22:10:04 +0530 Subject: [PATCH 1/4] feat(python): add term entry for 'with' keyword with syntax, examples, and codebyte (#7738) --- .../concepts/keywords/terms/with/with.md | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 content/python/concepts/keywords/terms/with/with.md diff --git a/content/python/concepts/keywords/terms/with/with.md b/content/python/concepts/keywords/terms/with/with.md new file mode 100644 index 00000000000..3337e828ef5 --- /dev/null +++ b/content/python/concepts/keywords/terms/with/with.md @@ -0,0 +1,83 @@ +--- +Title: 'with' +Description: 'Simplifies resource management by automatically handling setup and teardown actions using context managers.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Context Managers' + - 'File Handling' + - 'Resource Management' + - 'Python' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`with`** keyword in Python is used to wrap the execution of a block of code within methods defined by a **context manager**. +It simplifies **resource management** — automatically handling setup and teardown operations like opening and closing files, acquiring and releasing locks, or connecting and disconnecting from databases. + +When a block under `with` is executed: +1. The context manager’s `__enter__()` method is called. +2. The block of code inside the `with` statement runs. +3. The context manager’s `__exit__()` method is automatically called after the block finishes — even if an error occurs. + +## Syntax + +```pseudo +with expression [as variable]: + # Code block +``` + +**Parameters:** + +expression: A context manager that defines __enter__() and __exit__() methods. + +variable (optional): The object returned by the __enter__() method, which can be used inside the code block. + +**Return value:** + +The value returned by the context manager’s __enter__() method (if assigned using as). + +## Example: Managing File Resources +```py +with open("example.txt", "w") as file: + file.write("Hello, World!") +``` + +Output: +```shell +# (No visible output) +# The file 'example.txt' is written and automatically closed. +``` + +In this example: + +- The file is automatically opened and closed using with. +- There’s no need to manually call file.close(). +- Even if an exception occurs while writing, the file will still close properly. + +## Example: Using Multiple Context Managers +You can manage more than one resource in a single with statement: +```py +with open("input.txt", "r") as infile, open("output.txt", "w") as outfile: + data = infile.read() + outfile.write(data) +``` + +This ensures both files are safely opened and closed automatically. + +## Codebyte Example: File Handling with `with` +```codebyte/python +# Writing to a file using 'with' +with open("demo.txt", "w") as file: + file.write("Learning Python 'with' keyword!") + +# Reading the same file +with open("demo.txt", "r") as file: + content = file.read() +print(content) +``` + +**Expected Output:** +Learning Python 'with' keyword! \ No newline at end of file From 921d896e4ffd79781c6e82e75b5ebc60af8cea98 Mon Sep 17 00:00:00 2001 From: vansh2408 Date: Thu, 16 Oct 2025 22:20:19 +0530 Subject: [PATCH 2/4] Prettier fix --- .../python/concepts/keywords/terms/with/with.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/keywords/terms/with/with.md b/content/python/concepts/keywords/terms/with/with.md index 3337e828ef5..e9bb3e0b2f1 100644 --- a/content/python/concepts/keywords/terms/with/with.md +++ b/content/python/concepts/keywords/terms/with/with.md @@ -18,6 +18,7 @@ The **`with`** keyword in Python is used to wrap the execution of a block of cod It simplifies **resource management** — automatically handling setup and teardown operations like opening and closing files, acquiring and releasing locks, or connecting and disconnecting from databases. When a block under `with` is executed: + 1. The context manager’s `__enter__()` method is called. 2. The block of code inside the `with` statement runs. 3. The context manager’s `__exit__()` method is automatically called after the block finishes — even if an error occurs. @@ -31,21 +32,23 @@ with expression [as variable]: **Parameters:** -expression: A context manager that defines __enter__() and __exit__() methods. +expression: A context manager that defines **enter**() and **exit**() methods. -variable (optional): The object returned by the __enter__() method, which can be used inside the code block. +variable (optional): The object returned by the **enter**() method, which can be used inside the code block. **Return value:** -The value returned by the context manager’s __enter__() method (if assigned using as). +The value returned by the context manager’s **enter**() method (if assigned using as). ## Example: Managing File Resources + ```py with open("example.txt", "w") as file: file.write("Hello, World!") ``` Output: + ```shell # (No visible output) # The file 'example.txt' is written and automatically closed. @@ -57,8 +60,10 @@ In this example: - There’s no need to manually call file.close(). - Even if an exception occurs while writing, the file will still close properly. -## Example: Using Multiple Context Managers +## Example: Using Multiple Context Managers + You can manage more than one resource in a single with statement: + ```py with open("input.txt", "r") as infile, open("output.txt", "w") as outfile: data = infile.read() @@ -68,6 +73,7 @@ with open("input.txt", "r") as infile, open("output.txt", "w") as outfile: This ensures both files are safely opened and closed automatically. ## Codebyte Example: File Handling with `with` + ```codebyte/python # Writing to a file using 'with' with open("demo.txt", "w") as file: @@ -80,4 +86,4 @@ print(content) ``` **Expected Output:** -Learning Python 'with' keyword! \ No newline at end of file +Learning Python 'with' keyword! From ee98b212992154a2ef6e06b9582691a5e2843cda Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 20 Oct 2025 15:50:59 +0530 Subject: [PATCH 3/4] Refine 'with' keyword documentation in Python Updated the documentation for the 'with' keyword in Python. Improved clarity by refining explanations and examples, and corrected method names. --- .../concepts/keywords/terms/with/with.md | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/content/python/concepts/keywords/terms/with/with.md b/content/python/concepts/keywords/terms/with/with.md index e9bb3e0b2f1..381ebf57f3b 100644 --- a/content/python/concepts/keywords/terms/with/with.md +++ b/content/python/concepts/keywords/terms/with/with.md @@ -5,17 +5,14 @@ Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'Context Managers' - - 'File Handling' - - 'Resource Management' + - 'Files' - 'Python' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The **`with`** keyword in Python is used to wrap the execution of a block of code within methods defined by a **context manager**. -It simplifies **resource management** — automatically handling setup and teardown operations like opening and closing files, acquiring and releasing locks, or connecting and disconnecting from databases. +The **`with`** keyword in Python is used to wrap the execution of a block of code within methods defined by a context manager. It simplifies resource management — automatically handling setup and teardown operations like opening and closing files, acquiring and releasing locks, or connecting and disconnecting from databases. When a block under `with` is executed: @@ -27,63 +24,65 @@ When a block under `with` is executed: ```pseudo with expression [as variable]: - # Code block + # Code block ``` **Parameters:** -expression: A context manager that defines **enter**() and **exit**() methods. - -variable (optional): The object returned by the **enter**() method, which can be used inside the code block. +- `expression`: A context manager that defines `__enter__()` and `__exit__()` methods. +- `variable` (optional): The object returned by the `__enter__()` method, which can be used inside the code block. **Return value:** -The value returned by the context manager’s **enter**() method (if assigned using as). +The value returned by the context manager’s `__enter__()` method, if assigned using `as`. + +## Example 1: Managing File Resources -## Example: Managing File Resources +In this example, a file is opened, written to, and automatically closed after the block finishes: ```py with open("example.txt", "w") as file: - file.write("Hello, World!") + file.write("Hello, World!") ``` -Output: +The output of this code is: ```shell # (No visible output) # The file 'example.txt' is written and automatically closed. ``` -In this example: +Here the file is automatically opened and closed using `with`. Even if an error occurs while writing, `file.close()` is called automatically. -- The file is automatically opened and closed using with. -- There’s no need to manually call file.close(). -- Even if an exception occurs while writing, the file will still close properly. +## Example 2: Using Multiple Context Managers -## Example: Using Multiple Context Managers - -You can manage more than one resource in a single with statement: +In this example, two files are managed simultaneously, ensuring both are safely opened and closed: ```py with open("input.txt", "r") as infile, open("output.txt", "w") as outfile: - data = infile.read() - outfile.write(data) + data = infile.read() + outfile.write(data) ``` -This ensures both files are safely opened and closed automatically. +In this example, both files are safely managed, opened at the start, and closed automatically when the block ends. + +## Example 3: File Handling with `with` -## Codebyte Example: File Handling with `with` +In this example, data is written to a file and then read back using separate `with` blocks for writing and reading: -```codebyte/python +```py # Writing to a file using 'with' with open("demo.txt", "w") as file: - file.write("Learning Python 'with' keyword!") + file.write("Learning Python 'with' keyword!") # Reading the same file with open("demo.txt", "r") as file: - content = file.read() + content = file.read() print(content) ``` -**Expected Output:** +The expected output is: + +```shell Learning Python 'with' keyword! +``` From 8ab423cdda8c79fb0d45cadf829311fe4fb54b09 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 20 Oct 2025 15:51:56 +0530 Subject: [PATCH 4/4] Update return value explanation in with.md Clarify return value description for context manager. --- content/python/concepts/keywords/terms/with/with.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/with/with.md b/content/python/concepts/keywords/terms/with/with.md index 381ebf57f3b..78dfcc8eae2 100644 --- a/content/python/concepts/keywords/terms/with/with.md +++ b/content/python/concepts/keywords/terms/with/with.md @@ -34,7 +34,7 @@ with expression [as variable]: **Return value:** -The value returned by the context manager’s `__enter__()` method, if assigned using `as`. +Returns the object provided by the context manager’s `__enter__()` method, if assigned using `as`. ## Example 1: Managing File Resources