From e21712cd307883dcf6fae2c2249f294b857838eb Mon Sep 17 00:00:00 2001 From: "Joe S. Boyle" Date: Sat, 24 Dec 2022 13:29:46 +0000 Subject: [PATCH 1/3] Modernize the 'data-record class' example in the classes documentation --- Doc/tutorial/classes.rst | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 0e5a9402bc50e3..3508668ac3688a 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -737,19 +737,23 @@ to code that is byte-compiled together. The same restriction applies to Odds and Ends ============= -Sometimes it is useful to have a data type similar to the Pascal "record" or C -"struct", bundling together a few named data items. An empty class definition -will do nicely:: +"struct", bundling together a few named data items. The idiomatic approach +is to use :mod:`dataclasses` for this purpose:: - class Employee: - pass + from dataclasses import dataclasses - john = Employee() # Create an empty employee record + @dataclass + class Employee: + name: str + dept: str + salary: int - # Fill the fields of the record - john.name = 'John Doe' - john.dept = 'computer lab' - john.salary = 1000 + john = Employee("john", "computer lab", 1000) + + >>> john.dept + "computer lab" + >>> john.salary + 1000 A piece of Python code that expects a particular abstract data type can often be passed a class that emulates the methods of that data type instead. For From 8f16465e3c2be830bb635201f2bdce5fa950da12 Mon Sep 17 00:00:00 2001 From: "Joe S. Boyle" Date: Sat, 24 Dec 2022 15:02:34 +0000 Subject: [PATCH 2/3] Split the dataclass example in two, with the latter part being a REPL --- Doc/tutorial/classes.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 3508668ac3688a..96d8b56cdb6cff 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -737,6 +737,7 @@ to code that is byte-compiled together. The same restriction applies to Odds and Ends ============= +Sometimes it is useful to have a data type similar to the Pascal "record" or C "struct", bundling together a few named data items. The idiomatic approach is to use :mod:`dataclasses` for this purpose:: @@ -748,10 +749,11 @@ is to use :mod:`dataclasses` for this purpose:: dept: str salary: int - john = Employee("john", "computer lab", 1000) +:: + >>> john = Employee("john", "computer lab", 1000) >>> john.dept - "computer lab" + 'computer lab' >>> john.salary 1000 From 38af6b6a3996a958fe1488db724394734e225c10 Mon Sep 17 00:00:00 2001 From: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com> Date: Sat, 24 Dec 2022 15:08:18 +0000 Subject: [PATCH 3/3] Update example to use single quotes Co-authored-by: Alex Waygood --- Doc/tutorial/classes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 96d8b56cdb6cff..a206ba37197609 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -751,7 +751,7 @@ is to use :mod:`dataclasses` for this purpose:: :: - >>> john = Employee("john", "computer lab", 1000) + >>> john = Employee('john', 'computer lab', 1000) >>> john.dept 'computer lab' >>> john.salary