From 7c7f5153e234bf7a1a5e6b2a7e797f232f2a7af4 Mon Sep 17 00:00:00 2001 From: XinYuDu-AI <112471882+XinYuDu-AI@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:17:31 +0800 Subject: [PATCH] gh-162868: Fix the `collections.namedtuple` examples (GH-162869) (cherry picked from commit 114f465d12ac5df7a78f021de02b6614d1ae15d4) Co-authored-by: XinYuDu-AI <112471882+XinYuDu-AI@users.noreply.github.com> Co-authored-by: Stan Ulbrych --- Doc/library/collections.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index eaa69d1da8f250..a7e394e0e52c71 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -936,8 +936,9 @@ by the :mod:`csv` or :mod:`sqlite3` modules:: EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv - for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): - print(emp.name, emp.title) + with open("employees.csv", newline="") as f: + for emp in map(EmployeeRecord._make, csv.reader(f)): + print(emp.name, emp.title) import sqlite3 conn = sqlite3.connect('/companydata') @@ -945,6 +946,7 @@ by the :mod:`csv` or :mod:`sqlite3` modules:: cursor.execute('SELECT name, age, title, department, paygrade FROM employees') for emp in map(EmployeeRecord._make, cursor.fetchall()): print(emp.name, emp.title) + conn.close() In addition to the methods inherited from tuples, named tuples support three additional methods and two attributes. To prevent conflicts with