From de102b127ed97bb4fecc50f84d3a817a838f15bd Mon Sep 17 00:00:00 2001 From: Joe Boyle Date: Thu, 22 Dec 2022 13:59:39 +0000 Subject: [PATCH 1/3] 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 0e5a9402bc50e3..37bf5a95750323 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 From f97869a88eed0656f8f917f22528125e93de3da5 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Thu, 22 Dec 2022 22:32:41 -0600 Subject: [PATCH 2/3] Newline to separate interactive section, like above examples --- Doc/tutorial/classes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 37bf5a95750323..9f4a695658a0b7 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -750,6 +750,7 @@ is to use :mod:`dataclasses` for this purpose:: salary: int john = Employee("john", "computer lab", 1000) + >>> john.dept # "computer lab" >>> john.salary From 3021b7c29bd6b613eb23446a43bfedf37fa36a86 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Thu, 22 Dec 2022 22:33:50 -0600 Subject: [PATCH 3/3] Use interpreter output, not comments, like with Dog example --- Doc/tutorial/classes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 9f4a695658a0b7..54540df3f64f47 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -752,9 +752,9 @@ is to use :mod:`dataclasses` for this purpose:: john = Employee("john", "computer lab", 1000) >>> john.dept - # "computer lab" + "computer lab" >>> john.salary - # '1000' + 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy