8000 gh-99908 Update 'odds and ends' `class` documentation by JosephSBoyle · Pull Request #100498 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99908 Update 'odds and ends' class documentation #100498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-99908 Update class documentation to use a modernized example
  • Loading branch information
Joe Boyle authored and JosephSBoyle committed Dec 24, 2022
commit de102b127ed97bb4fecc50f84d3a817a838f15bd
28 changes: 16 additions & 12 deletions Doc/tutorial/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0