From 299ecee846761ece58d8e4ff9f31dfa75d3ab09a Mon Sep 17 00:00:00 2001 From: Joe Boyle Date: Thu, 22 Dec 2022 13:59:39 +0000 Subject: [PATCH] gh-99908 Update class documentation to use a modernized example --- Doc/tutorial/classes.rst | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 0e5a9402bc50e32..37bf5a957503237 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -738,18 +738,22 @@ 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:: - - class Employee: - pass - - john = Employee() # Create an empty employee record - - # Fill the fields of the record - john.name = 'John Doe' - john.dept = 'computer lab' - john.salary = 1000 +"struct", bundling together a few named data items. The idiomatic approach +is to use :mod:`dataclasses` for this purpose:: + + from dataclasses import dataclasses + + @dataclass + class Employee: + name: str + dept: str + salary: int + + 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