Skip to content

Commit 278affa

Browse files
author
Joe Boyle
committed
gh-99908 Update class documentation to use a modernized example
1 parent 4cc63e0 commit 278affa

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Doc/tutorial/classes.rst

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -738,18 +738,22 @@ Odds and Ends
738738
=============
739739

740740
Sometimes it is useful to have a data type similar to the Pascal "record" or C
741-
"struct", bundling together a few named data items. An empty class definition
742-
will do nicely::
743-
744-
class Employee:
745-
pass
746-
747-
john = Employee() # Create an empty employee record
748-
749-
# Fill the fields of the record
750-
john.name = 'John Doe'
751-
john.dept = 'computer lab'
752-
john.salary = 1000
741+
"struct", bundling together a few named data items. The idiomatic approach
742+
is to use :mod:`dataclasses` for this purpose::
743+
744+
from dataclasses import dataclasses
745+
746+
@dataclass
747+
class Employee:
748+
name: str
749+
dept: str
750+
salary: int
751+
752+
john = Employee("john", "computer lab", 1000)
753+
>>> john.dept
754+
# "computer lab"
755+
>>> john.salary
756+
# '1000'
753757

754758
A piece of Python code that expects a particular abstract data type can often be
755759
passed a class that emulates the methods of that data type instead. For

0 commit comments

Comments
 (0)