FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-99908 Update 'odds and ends' `class` documentation by JosephSBoyle · Pull Request #100498 · python/cpython · GitHub

/ cpython Public
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rst  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 14 additions & 9 deletions Doc/tutorial/classes.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,23 @@ 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
Expand Down

Back | FazBrowse Home | New Git URL