Linux iad1-shared-b7-18 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
Apache
: 67.205.6.31 | : 216.73.216.47
Cant Read [ /etc/named.conf ]
8.2.29
fernandoquevedo
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python3.10 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
70.96
KB
-rw-r--r--
__future__.html
20.75
KB
-rw-r--r--
__main__.html
44.87
KB
-rw-r--r--
_thread.html
30.39
KB
-rw-r--r--
abc.html
46.87
KB
-rw-r--r--
aifc.html
35.56
KB
-rw-r--r--
allos.html
34.33
KB
-rw-r--r--
archiving.html
17
KB
-rw-r--r--
argparse.html
300.89
KB
-rw-r--r--
array.html
37.28
KB
-rw-r--r--
ast.html
293.08
KB
-rw-r--r--
asynchat.html
37.41
KB
-rw-r--r--
asyncio-api-index.html
27.68
KB
-rw-r--r--
asyncio-dev.html
36.1
KB
-rw-r--r--
asyncio-eventloop.html
204.43
KB
-rw-r--r--
asyncio-exceptions.html
17.11
KB
-rw-r--r--
asyncio-future.html
38.23
KB
-rw-r--r--
asyncio-llapi-index.html
61.97
KB
-rw-r--r--
asyncio-platforms.html
21.77
KB
-rw-r--r--
asyncio-policy.html
39.56
KB
-rw-r--r--
asyncio-protocol.html
118.33
KB
-rw-r--r--
asyncio-queue.html
31.86
KB
-rw-r--r--
asyncio-stream.html
69.98
KB
-rw-r--r--
asyncio-subprocess.html
49.25
KB
-rw-r--r--
asyncio-sync.html
42.79
KB
-rw-r--r--
asyncio-task.html
109.14
KB
-rw-r--r--
asyncio.html
17.31
KB
-rw-r--r--
asyncore.html
49.36
KB
-rw-r--r--
atexit.html
22.87
KB
-rw-r--r--
audioop.html
46
KB
-rw-r--r--
audit_events.html
72.85
KB
-rw-r--r--
base64.html
48.09
KB
-rw-r--r--
bdb.html
66
KB
-rw-r--r--
binary.html
15.97
KB
-rw-r--r--
binascii.html
36.3
KB
-rw-r--r--
binhex.html
15.97
KB
-rw-r--r--
bisect.html
43.38
KB
-rw-r--r--
builtins.html
14.87
KB
-rw-r--r--
bz2.html
50.68
KB
-rw-r--r--
calendar.html
69.35
KB
-rw-r--r--
cgi.html
59.95
KB
-rw-r--r--
cgitb.html
19.96
KB
-rw-r--r--
chunk.html
22.21
KB
-rw-r--r--
cmath.html
43.89
KB
-rw-r--r--
cmd.html
50.42
KB
-rw-r--r--
code.html
35.38
KB
-rw-r--r--
codecs.html
157.96
KB
-rw-r--r--
codeop.html
18.26
KB
-rw-r--r--
collections.abc.html
79.17
KB
-rw-r--r--
collections.html
189.43
KB
-rw-r--r--
colorsys.html
17.31
KB
-rw-r--r--
compileall.html
50.68
KB
-rw-r--r--
concurrency.html
24.15
KB
-rw-r--r--
concurrent.futures.html
79.03
KB
-rw-r--r--
concurrent.html
11.56
KB
-rw-r--r--
configparser.html
159.4
KB
-rw-r--r--
constants.html
22.41
KB
-rw-r--r--
contextlib.html
117.2
KB
-rw-r--r--
contextvars.html
38.38
KB
-rw-r--r--
copy.html
17.92
KB
-rw-r--r--
copyreg.html
18.63
KB
-rw-r--r--
crypt.html
28.29
KB
-rw-r--r--
crypto.html
13.93
KB
-rw-r--r--
csv.html
82.89
KB
-rw-r--r--
ctypes.html
295.49
KB
-rw-r--r--
curses.ascii.html
32.04
KB
-rw-r--r--
curses.html
221.24
KB
-rw-r--r--
curses.panel.html
22.16
KB
-rw-r--r--
custominterp.html
11.85
KB
-rw-r--r--
dataclasses.html
113.16
KB
-rw-r--r--
datatypes.html
31.82
KB
-rw-r--r--
datetime.html
374.92
KB
-rw-r--r--
dbm.html
51.16
KB
-rw-r--r--
debug.html
17
KB
-rw-r--r--
decimal.html
270.12
KB
-rw-r--r--
development.html
32.34
KB
-rw-r--r--
devmode.html
31.72
KB
-rw-r--r--
dialog.html
43.03
KB
-rw-r--r--
difflib.html
111.99
KB
-rw-r--r--
dis.html
136.07
KB
-rw-r--r--
distribution.html
13.67
KB
-rw-r--r--
distutils.html
14.73
KB
-rw-r--r--
doctest.html
199.32
KB
-rw-r--r--
email.charset.html
31.92
KB
-rw-r--r--
email.compat32-message.html
97.74
KB
-rw-r--r--
email.contentmanager.html
37.02
KB
-rw-r--r--
email.encoders.html
18.47
KB
-rw-r--r--
email.errors.html
24.45
KB
-rw-r--r--
email.examples.html
60.49
KB
-rw-r--r--
email.generator.html
48.79
KB
-rw-r--r--
email.header.html
37.61
KB
-rw-r--r--
email.headerregistry.html
66.26
KB
-rw-r--r--
email.html
30.75
KB
-rw-r--r--
email.iterators.html
18.23
KB
-rw-r--r--
email.message.html
99.75
KB
-rw-r--r--
email.mime.html
44.89
KB
-rw-r--r--
email.parser.html
57.39
KB
-rw-r--r--
email.policy.html
84.24
KB
-rw-r--r--
email.utils.html
42.57
KB
-rw-r--r--
ensurepip.html
23.9
KB
-rw-r--r--
enum.html
151
KB
-rw-r--r--
errno.html
60.17
KB
-rw-r--r--
exceptions.html
108.44
KB
-rw-r--r--
faulthandler.html
31.5
KB
-rw-r--r--
fcntl.html
36.67
KB
-rw-r--r--
filecmp.html
32.08
KB
-rw-r--r--
fileformats.html
13.52
KB
-rw-r--r--
fileinput.html
40.37
KB
-rw-r--r--
filesys.html
16.78
KB
-rw-r--r--
fnmatch.html
21.02
KB
-rw-r--r--
fractions.html
35.29
KB
-rw-r--r--
frameworks.html
16.15
KB
-rw-r--r--
ftplib.html
71.97
KB
-rw-r--r--
functional.html
12.19
KB
-rw-r--r--
functions.html
260.33
KB
-rw-r--r--
functools.html
92.96
KB
-rw-r--r--
gc.html
40.97
KB
-rw-r--r--
getopt.html
29.4
KB
-rw-r--r--
getpass.html
16.01
KB
-rw-r--r--
gettext.html
98.02
KB
-rw-r--r--
glob.html
27.2
KB
-rw-r--r--
graphlib.html
34.82
KB
-rw-r--r--
grp.html
16.03
KB
-rw-r--r--
gzip.html
45.65
KB
-rw-r--r--
hashlib.html
93.19
KB
-rw-r--r--
heapq.html
42.43
KB
-rw-r--r--
hmac.html
24.74
KB
-rw-r--r--
html.entities.html
14.91
KB
-rw-r--r--
html.html
14.81
KB
-rw-r--r--
html.parser.html
47.7
KB
-rw-r--r--
http.client.html
85.63
KB
-rw-r--r--
http.cookiejar.html
113.94
KB
-rw-r--r--
http.cookies.html
47.64
KB
-rw-r--r--
http.html
45.52
KB
-rw-r--r--
http.server.html
73.05
KB
-rw-r--r--
i18n.html
13.44
KB
-rw-r--r--
idle.html
77.18
KB
-rw-r--r--
imaplib.html
87.08
KB
-rw-r--r--
imghdr.html
17.94
KB
-rw-r--r--
imp.html
54.32
KB
-rw-r--r--
importlib.html
239.96
KB
-rw-r--r--
importlib.metadata.html
43.14
KB
-rw-r--r--
index.html
75.06
KB
-rw-r--r--
inspect.html
168.74
KB
-rw-r--r--
internet.html
29.89
KB
-rw-r--r--
intro.html
13.81
KB
-rw-r--r--
io.html
154.15
KB
-rw-r--r--
ipaddress.html
137.75
KB
-rw-r--r--
ipc.html
12.6
KB
-rw-r--r--
itertools.html
153.27
KB
-rw-r--r--
json.html
98.9
KB
-rw-r--r--
keyword.html
14.88
KB
-rw-r--r--
language.html
16.71
KB
-rw-r--r--
linecache.html
18.09
KB
-rw-r--r--
locale.html
77.35
KB
-rw-r--r--
logging.config.html
100.3
KB
-rw-r--r--
logging.handlers.html
146.28
KB
-rw-r--r--
logging.html
169.67
KB
-rw-r--r--
lzma.html
66.89
KB
-rw-r--r--
mailbox.html
176.34
KB
-rw-r--r--
mailcap.html
20.86
KB
-rw-r--r--
markup.html
22.31
KB
-rw-r--r--
marshal.html
24.43
KB
-rw-r--r--
math.html
85.82
KB
-rw-r--r--
mimetypes.html
40.27
KB
-rw-r--r--
mm.html
11.16
KB
-rw-r--r--
mmap.html
55.53
KB
-rw-r--r--
modulefinder.html
23.67
KB
-rw-r--r--
modules.html
15.88
KB
-rw-r--r--
msilib.html
78.38
KB
-rw-r--r--
msvcrt.html
29.22
KB
-rw-r--r--
multiprocessing.html
405.24
KB
-rw-r--r--
multiprocessing.shared_memory....
60
KB
-rw-r--r--
netdata.html
20.24
KB
-rw-r--r--
netrc.html
20.39
KB
-rw-r--r--
nis.html
16.76
KB
-rw-r--r--
nntplib.html
84.93
KB
-rw-r--r--
numbers.html
45.67
KB
-rw-r--r--
numeric.html
19.29
KB
-rw-r--r--
operator.html
107.32
KB
-rw-r--r--
optparse.html
256.96
KB
-rw-r--r--
os.html
594.39
KB
-rw-r--r--
os.path.html
65.54
KB
-rw-r--r--
ossaudiodev.html
56.35
KB
-rw-r--r--
pathlib.html
170.01
KB
-rw-r--r--
pdb.html
68.67
KB
-rw-r--r--
persistence.html
19.13
KB
-rw-r--r--
pickle.html
154.45
KB
-rw-r--r--
pickletools.html
22.92
KB
-rw-r--r--
pipes.html
21.7
KB
-rw-r--r--
pkgutil.html
42.76
KB
-rw-r--r--
platform.html
45.08
KB
-rw-r--r--
plistlib.html
30.06
KB
-rw-r--r--
poplib.html
41.42
KB
-rw-r--r--
posix.html
20.96
KB
-rw-r--r--
pprint.html
55.35
KB
-rw-r--r--
profile.html
84.63
KB
-rw-r--r--
pty.html
23.7
KB
-rw-r--r--
pwd.html
16.84
KB
-rw-r--r--
py_compile.html
29
KB
-rw-r--r--
pyclbr.html
24.06
KB
-rw-r--r--
pydoc.html
19.59
KB
-rw-r--r--
pyexpat.html
104.38
KB
-rw-r--r--
python.html
22.46
KB
-rw-r--r--
queue.html
43.39
KB
-rw-r--r--
quopri.html
18.64
KB
-rw-r--r--
random.html
86.31
KB
-rw-r--r--
re.html
214.24
KB
-rw-r--r--
readline.html
50.19
KB
-rw-r--r--
reprlib.html
30.85
KB
-rw-r--r--
resource.html
48.26
KB
-rw-r--r--
rlcompleter.html
18.06
KB
-rw-r--r--
runpy.html
30.99
KB
-rw-r--r--
sched.html
27.04
KB
-rw-r--r--
secrets.html
30.2
KB
-rw-r--r--
security_warnings.html
16.21
KB
-rw-r--r--
select.html
74.58
KB
-rw-r--r--
selectors.html
41.91
KB
-rw-r--r--
shelve.html
36.54
KB
-rw-r--r--
shlex.html
61.03
KB
-rw-r--r--
shutil.html
100.13
KB
-rw-r--r--
signal.html
95.39
KB
-rw-r--r--
site.html
38.16
KB
-rw-r--r--
smtpd.html
44.09
KB
-rw-r--r--
smtplib.html
81.4
KB
-rw-r--r--
sndhdr.html
16.4
KB
-rw-r--r--
socket.html
256.95
KB
-rw-r--r--
socketserver.html
91.95
KB
-rw-r--r--
spwd.html
16.71
KB
-rw-r--r--
sqlite3.html
225.51
KB
-rw-r--r--
ssl.html
328.83
KB
-rw-r--r--
stat.html
57.79
KB
-rw-r--r--
statistics.html
123.22
KB
-rw-r--r--
stdtypes.html
615.43
KB
-rw-r--r--
string.html
108.03
KB
-rw-r--r--
stringprep.html
24.94
KB
-rw-r--r--
struct.html
70.58
KB
-rw-r--r--
subprocess.html
199.03
KB
-rw-r--r--
sunau.html
39.84
KB
-rw-r--r--
superseded.html
26.21
KB
-rw-r--r--
symtable.html
33.26
KB
-rw-r--r--
sys.html
198.83
KB
-rw-r--r--
sysconfig.html
39.81
KB
-rw-r--r--
syslog.html
27.36
KB
-rw-r--r--
tabnanny.html
16.18
KB
-rw-r--r--
tarfile.html
161.92
KB
-rw-r--r--
telnetlib.html
38.21
KB
-rw-r--r--
tempfile.html
58.39
KB
-rw-r--r--
termios.html
22.86
KB
-rw-r--r--
test.html
222.85
KB
-rw-r--r--
text.html
16.8
KB
-rw-r--r--
textwrap.html
48.83
KB
-rw-r--r--
threading.html
128.33
KB
-rw-r--r--
time.html
107.46
KB
-rw-r--r--
timeit.html
52.63
KB
-rw-r--r--
tk.html
28.08
KB
-rw-r--r--
tkinter.colorchooser.html
14.49
KB
-rw-r--r--
tkinter.dnd.html
17.09
KB
-rw-r--r--
tkinter.font.html
22.21
KB
-rw-r--r--
tkinter.html
106.85
KB
-rw-r--r--
tkinter.messagebox.html
20.09
KB
-rw-r--r--
tkinter.scrolledtext.html
14.52
KB
-rw-r--r--
tkinter.tix.html
59.69
KB
-rw-r--r--
tkinter.ttk.html
138.37
KB
-rw-r--r--
token.html
45.73
KB
-rw-r--r--
tokenize.html
39.86
KB
-rw-r--r--
trace.html
38.85
KB
-rw-r--r--
traceback.html
75.61
KB
-rw-r--r--
tracemalloc.html
122.33
KB
-rw-r--r--
tty.html
14.46
KB
-rw-r--r--
turtle.html
271.71
KB
-rw-r--r--
types.html
64.85
KB
-rw-r--r--
typing.html
315.69
KB
-rw-r--r--
unicodedata.html
27.3
KB
-rw-r--r--
unittest.html
310.01
KB
-rw-r--r--
unittest.mock-examples.html
177.75
KB
-rw-r--r--
unittest.mock.html
363.51
KB
-rw-r--r--
unix.html
13.46
KB
-rw-r--r--
urllib.error.html
18.56
KB
-rw-r--r--
urllib.html
12.92
KB
-rw-r--r--
urllib.parse.html
104.65
KB
-rw-r--r--
urllib.request.html
208.87
KB
-rw-r--r--
urllib.robotparser.html
22.63
KB
-rw-r--r--
uu.html
18.2
KB
-rw-r--r--
uuid.html
42.88
KB
-rw-r--r--
venv.html
90.43
KB
-rw-r--r--
warnings.html
69.13
KB
-rw-r--r--
wave.html
36.12
KB
-rw-r--r--
weakref.html
73.36
KB
-rw-r--r--
webbrowser.html
33.98
KB
-rw-r--r--
windows.html
12.32
KB
-rw-r--r--
winreg.html
89.48
KB
-rw-r--r--
winsound.html
26.3
KB
-rw-r--r--
wsgiref.html
111.52
KB
-rw-r--r--
xdrlib.html
42.07
KB
-rw-r--r--
xml.dom.html
114.66
KB
-rw-r--r--
xml.dom.minidom.html
49.6
KB
-rw-r--r--
xml.dom.pulldom.html
30.06
KB
-rw-r--r--
xml.etree.elementtree.html
186.49
KB
-rw-r--r--
xml.html
22.34
KB
-rw-r--r--
xml.sax.handler.html
55.78
KB
-rw-r--r--
xml.sax.html
31.74
KB
-rw-r--r--
xml.sax.reader.html
56.23
KB
-rw-r--r--
xml.sax.utils.html
22.28
KB
-rw-r--r--
xmlrpc.client.html
79.73
KB
-rw-r--r--
xmlrpc.html
11.9
KB
-rw-r--r--
xmlrpc.server.html
63.56
KB
-rw-r--r--
zipapp.html
52.45
KB
-rw-r--r--
zipfile.html
116.59
KB
-rw-r--r--
zipimport.html
33.76
KB
-rw-r--r--
zlib.html
42.29
KB
-rw-r--r--
zoneinfo.html
57.25
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : dataclasses.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>dataclasses — Data Classes — Python 3.10.12 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?2022.1" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.10.12 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="contextlib — Utilities for with-statement contexts" href="contextlib.html" /> <link rel="prev" title="warnings — Warning control" href="warnings.html" /> <link rel="canonical" href="file:///usr/share/doc/python3.10/html/library/dataclasses.html" /> <style> @media only screen { table.full-width-table { width: 100%; } } </style> <link rel="shortcut icon" type="image/png" href="../_static/py.svg" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/menu.js"></script> </head> <body> <div class="mobile-nav"> <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation" aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" /> <label for="menuToggler" class="toggler__label"> <span></span> </label> <nav class="nav-content" role="navigation"> <a href="https://www.python.org/" class="nav-logo"> <img src="../_static/py.svg" alt="Logo"/> </a> <div class="version_switcher_placeholder"></div> <form role="search" class="search" action="../search.html" method="get"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon"> <path fill-rule="nonzero" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path> </svg> <input type="text" name="q" aria-label="Quick search"/> <input type="submit" value="Go"/> </form> </nav> <div class="menu-wrapper"> <nav class="menu" role="navigation" aria-label="main navigation"> <div class="language_switcher_placeholder"></div> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dataclasses</span></code> — Data Classes</a><ul> <li><a class="reference internal" href="#module-contents">Module contents</a></li> <li><a class="reference internal" href="#post-init-processing">Post-init processing</a></li> <li><a class="reference internal" href="#class-variables">Class variables</a></li> <li><a class="reference internal" href="#init-only-variables">Init-only variables</a></li> <li><a class="reference internal" href="#frozen-instances">Frozen instances</a></li> <li><a class="reference internal" href="#inheritance">Inheritance</a></li> <li><a class="reference internal" href="#re-ordering-of-keyword-only-parameters-in-init">Re-ordering of keyword-only parameters in <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a></li> <li><a class="reference internal" href="#default-factory-functions">Default factory functions</a></li> <li><a class="reference internal" href="#mutable-default-values">Mutable default values</a></li> <li><a class="reference internal" href="#descriptor-typed-fields">Descriptor-typed fields</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="warnings.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code> — Warning control</a></p> <h4>Next topic</h4> <p class="topless"><a href="contextlib.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">contextlib</span></code> — Utilities for <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code>-statement contexts</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/dataclasses.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </nav> </div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="contextlib.html" title="contextlib — Utilities for with-statement contexts" accesskey="N">next</a> |</li> <li class="right" > <a href="warnings.html" title="warnings — Warning control" accesskey="P">previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">dataclasses</span></code> — Data Classes</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-dataclasses"> <span id="dataclasses-data-classes"></span><h1><a class="reference internal" href="#module-dataclasses" title="dataclasses: Generate special methods on user-defined classes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dataclasses</span></code></a> — Data Classes<a class="headerlink" href="#module-dataclasses" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.10/Lib/dataclasses.py">Lib/dataclasses.py</a></p> <hr class="docutils" /> <p>This module provides a decorator and functions for automatically adding generated <a class="reference internal" href="../glossary.html#term-special-method"><span class="xref std std-term">special method</span></a>s such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> to user-defined classes. It was originally described in <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0557"><strong>PEP 557</strong></a>.</p> <p>The member variables to use in these generated methods are defined using <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0526"><strong>PEP 526</strong></a> type annotations. For example, this code:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">InventoryItem</span><span class="p">:</span> <span class="sd">"""Class for keeping track of an item in inventory."""</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="n">unit_price</span><span class="p">:</span> <span class="nb">float</span> <span class="n">quantity_on_hand</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">total_cost</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">float</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit_price</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">quantity_on_hand</span> </pre></div> </div> <p>will add, among other things, a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> that looks like:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">unit_price</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">quantity_on_hand</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span> <span class="bp">self</span><span class="o">.</span><span class="n">unit_price</span> <span class="o">=</span> <span class="n">unit_price</span> <span class="bp">self</span><span class="o">.</span><span class="n">quantity_on_hand</span> <span class="o">=</span> <span class="n">quantity_on_hand</span> </pre></div> </div> <p>Note that this method is automatically added to the class: it is not directly specified in the <code class="docutils literal notranslate"><span class="pre">InventoryItem</span></code> definition shown above.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> <section id="module-contents"> <h2>Module contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.dataclass"> <span class="sig-prename descclassname"><span class="pre">@</span></span><span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">dataclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">repr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eq</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unsafe_hash</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">frozen</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">match_args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kw_only</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">slots</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.dataclass" title="Permalink to this definition">¶</a></dt> <dd><p>This function is a <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> that is used to add generated <a class="reference internal" href="../glossary.html#term-special-method"><span class="xref std std-term">special method</span></a>s to classes, as described below.</p> <p>The <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator examines the class to find <code class="docutils literal notranslate"><span class="pre">field</span></code>s. A <code class="docutils literal notranslate"><span class="pre">field</span></code> is defined as a class variable that has a <a class="reference internal" href="../glossary.html#term-variable-annotation"><span class="xref std std-term">type annotation</span></a>. With two exceptions described below, nothing in <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> examines the type specified in the variable annotation.</p> <p>The order of the fields in all of the generated methods is the order in which they appear in the class definition.</p> <p>The <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator will add various “dunder” methods to the class, described below. If any of the added methods already exist in the class, the behavior depends on the parameter, as documented below. The decorator returns the same class that it is called on; no new class is created.</p> <p>If <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> is used just as a simple decorator with no parameters, it acts as if it has the default values documented in this signature. That is, these three uses of <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> are equivalent:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="o">...</span> <span class="nd">@dataclass</span><span class="p">()</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="o">...</span> <span class="nd">@dataclass</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="nb">repr</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">eq</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">unsafe_hash</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">frozen</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">match_args</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">kw_only</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">slots</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="o">...</span> </pre></div> </div> <p>The parameters to <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> are:</p> <ul> <li><p><code class="docutils literal notranslate"><span class="pre">init</span></code>: If true (the default), a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method will be generated.</p> <p>If the class already defines <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code>, this parameter is ignored.</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">repr</span></code>: If true (the default), a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> method will be generated. The generated repr string will have the class name and the name and repr of each field, in the order they are defined in the class. Fields that are marked as being excluded from the repr are not included. For example: <code class="docutils literal notranslate"><span class="pre">InventoryItem(name='widget',</span> <span class="pre">unit_price=3.0,</span> <span class="pre">quantity_on_hand=10)</span></code>.</p> <p>If the class already defines <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code>, this parameter is ignored.</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">eq</span></code>: If true (the default), an <code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code> method will be generated. This method compares the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type.</p> <p>If the class already defines <code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code>, this parameter is ignored.</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">order</span></code>: If true (the default is <code class="docutils literal notranslate"><span class="pre">False</span></code>), <code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code> methods will be generated. These compare the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type. If <code class="docutils literal notranslate"><span class="pre">order</span></code> is true and <code class="docutils literal notranslate"><span class="pre">eq</span></code> is false, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p> <p>If the class already defines any of <code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code>, or <code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code>, then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">unsafe_hash</span></code>: If <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default), a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method is generated according to how <code class="docutils literal notranslate"><span class="pre">eq</span></code> and <code class="docutils literal notranslate"><span class="pre">frozen</span></code> are set.</p> <p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> is used by built-in <a class="reference internal" href="functions.html#hash" title="hash"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hash()</span></code></a>, and when objects are added to hashed collections such as dictionaries and sets. Having a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> implies that instances of the class are immutable. Mutability is a complicated property that depends on the programmer’s intent, the existence and behavior of <code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code>, and the values of the <code class="docutils literal notranslate"><span class="pre">eq</span></code> and <code class="docutils literal notranslate"><span class="pre">frozen</span></code> flags in the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator.</p> <p>By default, <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> will not implicitly add a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method unless it is safe to do so. Neither will it add or change an existing explicitly defined <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method. Setting the class attribute <code class="docutils literal notranslate"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">None</span></code> has a specific meaning to Python, as described in the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> documentation.</p> <p>If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> is not explicitly defined, or if it is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, then <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> <em>may</em> add an implicit <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method. Although not recommended, you can force <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> to create a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method with <code class="docutils literal notranslate"><span class="pre">unsafe_hash=True</span></code>. This might be the case if your class is logically immutable but can nonetheless be mutated. This is a specialized use case and should be considered carefully.</p> <p>Here are the rules governing implicit creation of a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method. Note that you cannot both have an explicit <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method in your dataclass and set <code class="docutils literal notranslate"><span class="pre">unsafe_hash=True</span></code>; this will result in a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p> <p>If <code class="docutils literal notranslate"><span class="pre">eq</span></code> and <code class="docutils literal notranslate"><span class="pre">frozen</span></code> are both true, by default <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> will generate a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method for you. If <code class="docutils literal notranslate"><span class="pre">eq</span></code> is true and <code class="docutils literal notranslate"><span class="pre">frozen</span></code> is false, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> will be set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, marking it unhashable (which it is, since it is mutable). If <code class="docutils literal notranslate"><span class="pre">eq</span></code> is false, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> will be left untouched meaning the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method of the superclass will be used (if the superclass is <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>, this means it will fall back to id-based hashing).</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">frozen</span></code>: If true (the default is <code class="docutils literal notranslate"><span class="pre">False</span></code>), assigning to fields will generate an exception. This emulates read-only frozen instances. If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">__delattr__()</span></code> is defined in the class, then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised. See the discussion below.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">match_args</span></code>: If true (the default is <code class="docutils literal notranslate"><span class="pre">True</span></code>), the <code class="docutils literal notranslate"><span class="pre">__match_args__</span></code> tuple will be created from the list of parameters to the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method (even if <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> is not generated, see above). If false, or if <code class="docutils literal notranslate"><span class="pre">__match_args__</span></code> is already defined in the class, then <code class="docutils literal notranslate"><span class="pre">__match_args__</span></code> will not be generated.</p></li> </ul> <blockquote> <div><div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </div></blockquote> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">kw_only</span></code>: If true (the default value is <code class="docutils literal notranslate"><span class="pre">False</span></code>), then all fields will be marked as keyword-only. If a field is marked as keyword-only, then the only affect is that the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> parameter generated from a keyword-only field must be specified with a keyword when <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> is called. There is no effect on any other aspect of dataclasses. See the <a class="reference internal" href="../glossary.html#term-parameter"><span class="xref std std-term">parameter</span></a> glossary entry for details. Also see the <a class="reference internal" href="#dataclasses.KW_ONLY" title="dataclasses.KW_ONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">KW_ONLY</span></code></a> section.</p></li> </ul> <blockquote> <div><div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </div></blockquote> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">slots</span></code>: If true (the default is <code class="docutils literal notranslate"><span class="pre">False</span></code>), <code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code> attribute will be generated and new class will be returned instead of the original one. If <code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code> is already defined in the class, then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p></li> </ul> <blockquote> <div><div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </div></blockquote> <p><code class="docutils literal notranslate"><span class="pre">field</span></code>s may optionally specify a default value, using normal Python syntax:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span> <span class="c1"># 'a' has no default value</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># assign a default value for 'b'</span> </pre></div> </div> <p>In this example, both <code class="docutils literal notranslate"><span class="pre">a</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code> will be included in the added <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method, which will be defined as:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">):</span> </pre></div> </div> <p><a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> will be raised if a field without a default value follows a field with a default value. This is true whether this occurs in a single class, or as a result of class inheritance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.field"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">field</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">MISSING</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">MISSING</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">repr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">hash</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compare</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">metadata</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kw_only</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">MISSING</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.field" title="Permalink to this definition">¶</a></dt> <dd><p>For common and simple use cases, no other functionality is required. There are, however, some dataclass features that require additional per-field information. To satisfy this need for additional information, you can replace the default field value with a call to the provided <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-func docutils literal notranslate"><span class="pre">field()</span></code></a> function. For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">mylist</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span> <span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span> <span class="n">c</span><span class="o">.</span><span class="n">mylist</span> <span class="o">+=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> </pre></div> </div> <p>As shown above, the <a class="reference internal" href="#dataclasses.MISSING" title="dataclasses.MISSING"><code class="xref py py-const docutils literal notranslate"><span class="pre">MISSING</span></code></a> value is a sentinel object used to detect if some parameters are provided by the user. This sentinel is used because <code class="docutils literal notranslate"><span class="pre">None</span></code> is a valid value for some parameters with a distinct meaning. No code should directly use the <a class="reference internal" href="#dataclasses.MISSING" title="dataclasses.MISSING"><code class="xref py py-const docutils literal notranslate"><span class="pre">MISSING</span></code></a> value.</p> <p>The parameters to <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-func docutils literal notranslate"><span class="pre">field()</span></code></a> are:</p> <ul> <li><p><code class="docutils literal notranslate"><span class="pre">default</span></code>: If provided, this will be the default value for this field. This is needed because the <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">field()</span></code></a> call itself replaces the normal position of the default value.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">default_factory</span></code>: If provided, it must be a zero-argument callable that will be called when a default value is needed for this field. Among other purposes, this can be used to specify fields with mutable default values, as discussed below. It is an error to specify both <code class="docutils literal notranslate"><span class="pre">default</span></code> and <code class="docutils literal notranslate"><span class="pre">default_factory</span></code>.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">init</span></code>: If true (the default), this field is included as a parameter to the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">repr</span></code>: If true (the default), this field is included in the string returned by the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> method.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">hash</span></code>: This can be a bool or <code class="docutils literal notranslate"><span class="pre">None</span></code>. If true, this field is included in the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code> method. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), use the value of <code class="docutils literal notranslate"><span class="pre">compare</span></code>: this would normally be the expected behavior. A field should be considered in the hash if it’s used for comparisons. Setting this value to anything other than <code class="docutils literal notranslate"><span class="pre">None</span></code> is discouraged.</p> <p>One possible reason to set <code class="docutils literal notranslate"><span class="pre">hash=False</span></code> but <code class="docutils literal notranslate"><span class="pre">compare=True</span></code> would be if a field is expensive to compute a hash value for, that field is needed for equality testing, and there are other fields that contribute to the type’s hash value. Even if a field is excluded from the hash, it will still be used for comparisons.</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">compare</span></code>: If true (the default), this field is included in the generated equality and comparison methods (<code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code>, et al.).</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">metadata</span></code>: This can be a mapping or None. None is treated as an empty dict. This value is wrapped in <a class="reference internal" href="types.html#types.MappingProxyType" title="types.MappingProxyType"><code class="xref py py-func docutils literal notranslate"><span class="pre">MappingProxyType()</span></code></a> to make it read-only, and exposed on the <a class="reference internal" href="#dataclasses.Field" title="dataclasses.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> object. It is not used at all by Data Classes, and is provided as a third-party extension mechanism. Multiple third-parties can each have their own key, to use as a namespace in the metadata.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">kw_only</span></code>: If true, this field will be marked as keyword-only. This is used when the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method’s parameters are computed.</p></li> </ul> <blockquote> <div><div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </div></blockquote> <p>If the default value of a field is specified by a call to <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-func docutils literal notranslate"><span class="pre">field()</span></code></a>, then the class attribute for this field will be replaced by the specified <code class="docutils literal notranslate"><span class="pre">default</span></code> value. If no <code class="docutils literal notranslate"><span class="pre">default</span></code> is provided, then the class attribute will be deleted. The intent is that after the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator runs, the class attributes will all contain the default values for the fields, just as if the default value itself were specified. For example, after:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="nb">repr</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="nb">repr</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span> <span class="n">t</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">20</span> </pre></div> </div> <p>The class attribute <code class="docutils literal notranslate"><span class="pre">C.z</span></code> will be <code class="docutils literal notranslate"><span class="pre">10</span></code>, the class attribute <code class="docutils literal notranslate"><span class="pre">C.t</span></code> will be <code class="docutils literal notranslate"><span class="pre">20</span></code>, and the class attributes <code class="docutils literal notranslate"><span class="pre">C.x</span></code> and <code class="docutils literal notranslate"><span class="pre">C.y</span></code> will not be set.</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="dataclasses.Field"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">Field</span></span><a class="headerlink" href="#dataclasses.Field" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#dataclasses.Field" title="dataclasses.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> objects describe each defined field. These objects are created internally, and are returned by the <a class="reference internal" href="#dataclasses.fields" title="dataclasses.fields"><code class="xref py py-func docutils literal notranslate"><span class="pre">fields()</span></code></a> module-level method (see below). Users should never instantiate a <a class="reference internal" href="#dataclasses.Field" title="dataclasses.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> object directly. Its documented attributes are:</p> <blockquote> <div><ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">name</span></code>: The name of the field.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: The type of the field.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">default</span></code>, <code class="docutils literal notranslate"><span class="pre">default_factory</span></code>, <code class="docutils literal notranslate"><span class="pre">init</span></code>, <code class="docutils literal notranslate"><span class="pre">repr</span></code>, <code class="docutils literal notranslate"><span class="pre">hash</span></code>, <code class="docutils literal notranslate"><span class="pre">compare</span></code>, <code class="docutils literal notranslate"><span class="pre">metadata</span></code>, and <code class="docutils literal notranslate"><span class="pre">kw_only</span></code> have the identical meaning and values as they do in the <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-func docutils literal notranslate"><span class="pre">field()</span></code></a> function.</p></li> </ul> </div></blockquote> <p>Other attributes may exist, but they are private and must not be inspected or relied on.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.fields"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">fields</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">class_or_instance</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.fields" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a tuple of <a class="reference internal" href="#dataclasses.Field" title="dataclasses.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> objects that define the fields for this dataclass. Accepts either a dataclass, or an instance of a dataclass. Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if not passed a dataclass or instance of one. Does not return pseudo-fields which are <code class="docutils literal notranslate"><span class="pre">ClassVar</span></code> or <code class="docutils literal notranslate"><span class="pre">InitVar</span></code>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.asdict"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">asdict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dict_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">dict</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.asdict" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the dataclass <code class="docutils literal notranslate"><span class="pre">obj</span></code> to a dict (by using the factory function <code class="docutils literal notranslate"><span class="pre">dict_factory</span></code>). Each dataclass is converted to a dict of its fields, as <code class="docutils literal notranslate"><span class="pre">name:</span> <span class="pre">value</span></code> pairs. dataclasses, dicts, lists, and tuples are recursed into. Other objects are copied with <a class="reference internal" href="copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.deepcopy()</span></code></a>.</p> <p>Example of using <a class="reference internal" href="#dataclasses.asdict" title="dataclasses.asdict"><code class="xref py py-func docutils literal notranslate"><span class="pre">asdict()</span></code></a> on nested dataclasses:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Point</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">mylist</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">Point</span><span class="p">]</span> <span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">assert</span> <span class="n">asdict</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s1">'y'</span><span class="p">:</span> <span class="mi">20</span><span class="p">}</span> <span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">([</span><span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">4</span><span class="p">)])</span> <span class="k">assert</span> <span class="n">asdict</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">'mylist'</span><span class="p">:</span> <span class="p">[{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">'y'</span><span class="p">:</span> <span class="mi">0</span><span class="p">},</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s1">'y'</span><span class="p">:</span> <span class="mi">4</span><span class="p">}]}</span> </pre></div> </div> <p>To create a shallow copy, the following workaround may be used:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">dict</span><span class="p">((</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">))</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">(</span><span class="n">obj</span><span class="p">))</span> </pre></div> </div> <p><a class="reference internal" href="#dataclasses.asdict" title="dataclasses.asdict"><code class="xref py py-func docutils literal notranslate"><span class="pre">asdict()</span></code></a> raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if <code class="docutils literal notranslate"><span class="pre">obj</span></code> is not a dataclass instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.astuple"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">astuple</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tuple_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">tuple</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.astuple" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the dataclass <code class="docutils literal notranslate"><span class="pre">obj</span></code> to a tuple (by using the factory function <code class="docutils literal notranslate"><span class="pre">tuple_factory</span></code>). Each dataclass is converted to a tuple of its field values. dataclasses, dicts, lists, and tuples are recursed into. Other objects are copied with <a class="reference internal" href="copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.deepcopy()</span></code></a>.</p> <p>Continuing from the previous example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="n">astuple</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">assert</span> <span class="n">astuple</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">==</span> <span class="p">([(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">4</span><span class="p">)],)</span> </pre></div> </div> <p>To create a shallow copy, the following workaround may be used:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">tuple</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">obj</span><span class="p">))</span> </pre></div> </div> <p><a class="reference internal" href="#dataclasses.astuple" title="dataclasses.astuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">astuple()</span></code></a> raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if <code class="docutils literal notranslate"><span class="pre">obj</span></code> is not a dataclass instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.make_dataclass"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">make_dataclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cls_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fields</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bases</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">repr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eq</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unsafe_hash</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">frozen</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">match_args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kw_only</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">slots</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.make_dataclass" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new dataclass with name <code class="docutils literal notranslate"><span class="pre">cls_name</span></code>, fields as defined in <code class="docutils literal notranslate"><span class="pre">fields</span></code>, base classes as given in <code class="docutils literal notranslate"><span class="pre">bases</span></code>, and initialized with a namespace as given in <code class="docutils literal notranslate"><span class="pre">namespace</span></code>. <code class="docutils literal notranslate"><span class="pre">fields</span></code> is an iterable whose elements are each either <code class="docutils literal notranslate"><span class="pre">name</span></code>, <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">type)</span></code>, or <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">type,</span> <span class="pre">Field)</span></code>. If just <code class="docutils literal notranslate"><span class="pre">name</span></code> is supplied, <code class="docutils literal notranslate"><span class="pre">typing.Any</span></code> is used for <code class="docutils literal notranslate"><span class="pre">type</span></code>. The values of <code class="docutils literal notranslate"><span class="pre">init</span></code>, <code class="docutils literal notranslate"><span class="pre">repr</span></code>, <code class="docutils literal notranslate"><span class="pre">eq</span></code>, <code class="docutils literal notranslate"><span class="pre">order</span></code>, <code class="docutils literal notranslate"><span class="pre">unsafe_hash</span></code>, <code class="docutils literal notranslate"><span class="pre">frozen</span></code>, <code class="docutils literal notranslate"><span class="pre">match_args</span></code>, <code class="docutils literal notranslate"><span class="pre">kw_only</span></code>, and <code class="docutils literal notranslate"><span class="pre">slots</span></code> have the same meaning as they do in <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a>.</p> <p>This function is not strictly required, because any Python mechanism for creating a new class with <code class="docutils literal notranslate"><span class="pre">__annotations__</span></code> can then apply the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> function to convert that class to a dataclass. This function is provided as a convenience. For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">C</span> <span class="o">=</span> <span class="n">make_dataclass</span><span class="p">(</span><span class="s1">'C'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'x'</span><span class="p">,</span> <span class="nb">int</span><span class="p">),</span> <span class="s1">'y'</span><span class="p">,</span> <span class="p">(</span><span class="s1">'z'</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">field</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">5</span><span class="p">))],</span> <span class="n">namespace</span><span class="o">=</span><span class="p">{</span><span class="s1">'add_one'</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">})</span> </pre></div> </div> <p>Is equivalent to:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="n">y</span><span class="p">:</span> <span class="s1">'typing.Any'</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">5</span> <span class="k">def</span> <span class="nf">add_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> </pre></div> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.replace"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">changes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new object of the same type as <code class="docutils literal notranslate"><span class="pre">obj</span></code>, replacing fields with values from <code class="docutils literal notranslate"><span class="pre">changes</span></code>. If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is not a Data Class, raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. If values in <code class="docutils literal notranslate"><span class="pre">changes</span></code> do not specify fields, raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p> <p>The newly returned object is created by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method of the dataclass. This ensures that <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code>, if present, is also called.</p> <p>Init-only variables without default values, if any exist, must be specified on the call to <a class="reference internal" href="#dataclasses.replace" title="dataclasses.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace()</span></code></a> so that they can be passed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code>.</p> <p>It is an error for <code class="docutils literal notranslate"><span class="pre">changes</span></code> to contain any fields that are defined as having <code class="docutils literal notranslate"><span class="pre">init=False</span></code>. A <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised in this case.</p> <p>Be forewarned about how <code class="docutils literal notranslate"><span class="pre">init=False</span></code> fields work during a call to <a class="reference internal" href="#dataclasses.replace" title="dataclasses.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace()</span></code></a>. They are not copied from the source object, but rather are initialized in <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code>, if they’re initialized at all. It is expected that <code class="docutils literal notranslate"><span class="pre">init=False</span></code> fields will be rarely and judiciously used. If they are used, it might be wise to have alternate class constructors, or perhaps a custom <code class="docutils literal notranslate"><span class="pre">replace()</span></code> (or similarly named) method which handles instance copying.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dataclasses.is_dataclass"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">is_dataclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#dataclasses.is_dataclass" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if its parameter is a dataclass or an instance of one, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>If you need to know if a class is an instance of a dataclass (and not a dataclass itself), then add a further check for <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">isinstance(obj,</span> <span class="pre">type)</span></code>:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">is_dataclass_instance</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span> <span class="k">return</span> <span class="n">is_dataclass</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="dataclasses.MISSING"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">MISSING</span></span><a class="headerlink" href="#dataclasses.MISSING" title="Permalink to this definition">¶</a></dt> <dd><p>A sentinel value signifying a missing default or default_factory.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="dataclasses.KW_ONLY"> <span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">KW_ONLY</span></span><a class="headerlink" href="#dataclasses.KW_ONLY" title="Permalink to this definition">¶</a></dt> <dd><p>A sentinel value used as a type annotation. Any fields after a pseudo-field with the type of <a class="reference internal" href="#dataclasses.KW_ONLY" title="dataclasses.KW_ONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">KW_ONLY</span></code></a> are marked as keyword-only fields. Note that a pseudo-field of type <a class="reference internal" href="#dataclasses.KW_ONLY" title="dataclasses.KW_ONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">KW_ONLY</span></code></a> is otherwise completely ignored. This includes the name of such a field. By convention, a name of <code class="docutils literal notranslate"><span class="pre">_</span></code> is used for a <a class="reference internal" href="#dataclasses.KW_ONLY" title="dataclasses.KW_ONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">KW_ONLY</span></code></a> field. Keyword-only fields signify <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> parameters that must be specified as keywords when the class is instantiated.</p> <p>In this example, the fields <code class="docutils literal notranslate"><span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">z</span></code> will be marked as keyword-only fields:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Point</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="nb">float</span> <span class="n">_</span><span class="p">:</span> <span class="n">KW_ONLY</span> <span class="n">y</span><span class="p">:</span> <span class="nb">float</span> <span class="n">z</span><span class="p">:</span> <span class="nb">float</span> <span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mf">1.5</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mf">2.0</span><span class="p">)</span> </pre></div> </div> <p>In a single dataclass, it is an error to specify more than one field whose type is <a class="reference internal" href="#dataclasses.KW_ONLY" title="dataclasses.KW_ONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">KW_ONLY</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd></dl> <dl class="py exception"> <dt class="sig sig-object py" id="dataclasses.FrozenInstanceError"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">dataclasses.</span></span><span class="sig-name descname"><span class="pre">FrozenInstanceError</span></span><a class="headerlink" href="#dataclasses.FrozenInstanceError" title="Permalink to this definition">¶</a></dt> <dd><p>Raised when an implicitly defined <code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">__delattr__()</span></code> is called on a dataclass which was defined with <code class="docutils literal notranslate"><span class="pre">frozen=True</span></code>. It is a subclass of <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>.</p> </dd></dl> </section> <section id="post-init-processing"> <h2>Post-init processing<a class="headerlink" href="#post-init-processing" title="Permalink to this headline">¶</a></h2> <p>The generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> code will call a method named <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code>, if <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code> is defined on the class. It will normally be called as <code class="docutils literal notranslate"><span class="pre">self.__post_init__()</span></code>. However, if any <code class="docutils literal notranslate"><span class="pre">InitVar</span></code> fields are defined, they will also be passed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code> in the order they were defined in the class. If no <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method is generated, then <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code> will not automatically be called.</p> <p>Among other uses, this allows for initializing field values that depend on one or more other fields. For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">a</span><span class="p">:</span> <span class="nb">float</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span> </pre></div> </div> <p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method generated by <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> does not call base class <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> methods. If the base class has an <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method that has to be called, it is common to call this method in a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code> method:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Rectangle</span><span class="p">:</span> <span class="n">height</span><span class="p">:</span> <span class="nb">float</span> <span class="n">width</span><span class="p">:</span> <span class="nb">float</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Square</span><span class="p">(</span><span class="n">Rectangle</span><span class="p">):</span> <span class="n">side</span><span class="p">:</span> <span class="nb">float</span> <span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">side</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">side</span><span class="p">)</span> </pre></div> </div> <p>Note, however, that in general the dataclass-generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> methods don’t need to be called, since the derived dataclass will take care of initializing all fields of any base class that is a dataclass itself.</p> <p>See the section below on init-only variables for ways to pass parameters to <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code>. Also see the warning about how <a class="reference internal" href="#dataclasses.replace" title="dataclasses.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace()</span></code></a> handles <code class="docutils literal notranslate"><span class="pre">init=False</span></code> fields.</p> </section> <section id="class-variables"> <h2>Class variables<a class="headerlink" href="#class-variables" title="Permalink to this headline">¶</a></h2> <p>One of the few places where <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> actually inspects the type of a field is to determine if a field is a class variable as defined in <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0526"><strong>PEP 526</strong></a>. It does this by checking if the type of the field is <code class="docutils literal notranslate"><span class="pre">typing.ClassVar</span></code>. If a field is a <code class="docutils literal notranslate"><span class="pre">ClassVar</span></code>, it is excluded from consideration as a field and is ignored by the dataclass mechanisms. Such <code class="docutils literal notranslate"><span class="pre">ClassVar</span></code> pseudo-fields are not returned by the module-level <a class="reference internal" href="#dataclasses.fields" title="dataclasses.fields"><code class="xref py py-func docutils literal notranslate"><span class="pre">fields()</span></code></a> function.</p> </section> <section id="init-only-variables"> <h2>Init-only variables<a class="headerlink" href="#init-only-variables" title="Permalink to this headline">¶</a></h2> <p>Another place where <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> inspects a type annotation is to determine if a field is an init-only variable. It does this by seeing if the type of a field is of type <code class="docutils literal notranslate"><span class="pre">dataclasses.InitVar</span></code>. If a field is an <code class="docutils literal notranslate"><span class="pre">InitVar</span></code>, it is considered a pseudo-field called an init-only field. As it is not a true field, it is not returned by the module-level <a class="reference internal" href="#dataclasses.fields" title="dataclasses.fields"><code class="xref py py-func docutils literal notranslate"><span class="pre">fields()</span></code></a> function. Init-only fields are added as parameters to the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method, and are passed to the optional <code class="xref py py-meth docutils literal notranslate"><span class="pre">__post_init__()</span></code> method. They are not otherwise used by dataclasses.</p> <p>For example, suppose a field will be initialized from a database, if a value is not provided when creating the class:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">i</span><span class="p">:</span> <span class="nb">int</span> <span class="n">j</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="n">database</span><span class="p">:</span> <span class="n">InitVar</span><span class="p">[</span><span class="n">DatabaseType</span> <span class="o">|</span> <span class="kc">None</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">database</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">j</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">database</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">j</span> <span class="o">=</span> <span class="n">database</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s1">'j'</span><span class="p">)</span> <span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">database</span><span class="o">=</span><span class="n">my_database</span><span class="p">)</span> </pre></div> </div> <p>In this case, <a class="reference internal" href="#dataclasses.fields" title="dataclasses.fields"><code class="xref py py-func docutils literal notranslate"><span class="pre">fields()</span></code></a> will return <a class="reference internal" href="#dataclasses.Field" title="dataclasses.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> objects for <code class="docutils literal notranslate"><span class="pre">i</span></code> and <code class="docutils literal notranslate"><span class="pre">j</span></code>, but not for <code class="docutils literal notranslate"><span class="pre">database</span></code>.</p> </section> <section id="frozen-instances"> <h2>Frozen instances<a class="headerlink" href="#frozen-instances" title="Permalink to this headline">¶</a></h2> <p>It is not possible to create truly immutable Python objects. However, by passing <code class="docutils literal notranslate"><span class="pre">frozen=True</span></code> to the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator you can emulate immutability. In that case, dataclasses will add <code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__delattr__()</span></code> methods to the class. These methods will raise a <a class="reference internal" href="#dataclasses.FrozenInstanceError" title="dataclasses.FrozenInstanceError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FrozenInstanceError</span></code></a> when invoked.</p> <p>There is a tiny performance penalty when using <code class="docutils literal notranslate"><span class="pre">frozen=True</span></code>: <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> cannot use simple assignment to initialize fields, and must use <a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__setattr__()</span></code></a>.</p> </section> <section id="inheritance"> <h2>Inheritance<a class="headerlink" href="#inheritance" title="Permalink to this headline">¶</a></h2> <p>When the dataclass is being created by the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator, it looks through all of the class’s base classes in reverse MRO (that is, starting at <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>) and, for each dataclass that it finds, adds the fields from that base class to an ordered mapping of fields. After all of the base class fields are added, it adds its own fields to the ordered mapping. All of the generated methods will use this combined, calculated ordered mapping of fields. Because the fields are in insertion order, derived classes override base classes. An example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Base</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="n">Any</span> <span class="o">=</span> <span class="mf">15.0</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">15</span> </pre></div> </div> <p>The final list of fields is, in order, <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">y</span></code>, <code class="docutils literal notranslate"><span class="pre">z</span></code>. The final type of <code class="docutils literal notranslate"><span class="pre">x</span></code> is <code class="docutils literal notranslate"><span class="pre">int</span></code>, as specified in class <code class="docutils literal notranslate"><span class="pre">C</span></code>.</p> <p>The generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method for <code class="docutils literal notranslate"><span class="pre">C</span></code> will look like:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">15</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">):</span> </pre></div> </div> </section> <section id="re-ordering-of-keyword-only-parameters-in-init"> <h2>Re-ordering of keyword-only parameters in <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code><a class="headerlink" href="#re-ordering-of-keyword-only-parameters-in-init" title="Permalink to this headline">¶</a></h2> <p>After the parameters needed for <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> are computed, any keyword-only parameters are moved to come after all regular (non-keyword-only) parameters. This is a requirement of how keyword-only parameters are implemented in Python: they must come after non-keyword-only parameters.</p> <p>In this example, <code class="docutils literal notranslate"><span class="pre">Base.y</span></code>, <code class="docutils literal notranslate"><span class="pre">Base.w</span></code>, and <code class="docutils literal notranslate"><span class="pre">D.t</span></code> are keyword-only fields, and <code class="docutils literal notranslate"><span class="pre">Base.x</span></code> and <code class="docutils literal notranslate"><span class="pre">D.z</span></code> are regular fields:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">Base</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="n">Any</span> <span class="o">=</span> <span class="mf">15.0</span> <span class="n">_</span><span class="p">:</span> <span class="n">KW_ONLY</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">w</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span> <span class="n">t</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> </pre></div> </div> <p>The generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method for <code class="docutils literal notranslate"><span class="pre">D</span></code> will look like:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="n">Any</span> <span class="o">=</span> <span class="mf">15.0</span><span class="p">,</span> <span class="n">z</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">w</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">t</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">):</span> </pre></div> </div> <p>Note that the parameters have been re-ordered from how they appear in the list of fields: parameters derived from regular fields are followed by parameters derived from keyword-only fields.</p> <p>The relative ordering of keyword-only parameters is maintained in the re-ordered <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> parameter list.</p> </section> <section id="default-factory-functions"> <h2>Default factory functions<a class="headerlink" href="#default-factory-functions" title="Permalink to this headline">¶</a></h2> <blockquote> <div><p>If a <a class="reference internal" href="#dataclasses.field" title="dataclasses.field"><code class="xref py py-func docutils literal notranslate"><span class="pre">field()</span></code></a> specifies a <code class="docutils literal notranslate"><span class="pre">default_factory</span></code>, it is called with zero arguments when a default value for the field is needed. For example, to create a new instance of a list, use:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">mylist</span><span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span> </pre></div> </div> <p>If a field is excluded from <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> (using <code class="docutils literal notranslate"><span class="pre">init=False</span></code>) and the field also specifies <code class="docutils literal notranslate"><span class="pre">default_factory</span></code>, then the default factory function will always be called from the generated <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> function. This happens because there is no other way to give the field an initial value.</p> </div></blockquote> </section> <section id="mutable-default-values"> <h2>Mutable default values<a class="headerlink" href="#mutable-default-values" title="Permalink to this headline">¶</a></h2> <blockquote> <div><p>Python stores default member variable values in class attributes. Consider this example, not using dataclasses:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">element</span><span class="p">)</span> <span class="n">o1</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span> <span class="n">o2</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span> <span class="n">o1</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">o2</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="k">assert</span> <span class="n">o1</span><span class="o">.</span><span class="n">x</span> <span class="o">==</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="k">assert</span> <span class="n">o1</span><span class="o">.</span><span class="n">x</span> <span class="ow">is</span> <span class="n">o2</span><span class="o">.</span><span class="n">x</span> </pre></div> </div> <p>Note that the two instances of class <code class="docutils literal notranslate"><span class="pre">C</span></code> share the same class variable <code class="docutils literal notranslate"><span class="pre">x</span></code>, as expected.</p> <p>Using dataclasses, <em>if</em> this code was valid:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">D</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="n">List</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="n">element</span> </pre></div> </div> <p>it would generate code similar to:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">D</span><span class="p">:</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="n">element</span> <span class="k">assert</span> <span class="n">D</span><span class="p">()</span><span class="o">.</span><span class="n">x</span> <span class="ow">is</span> <span class="n">D</span><span class="p">()</span><span class="o">.</span><span class="n">x</span> </pre></div> </div> <p>This has the same issue as the original example using class <code class="docutils literal notranslate"><span class="pre">C</span></code>. That is, two instances of class <code class="docutils literal notranslate"><span class="pre">D</span></code> that do not specify a value for <code class="docutils literal notranslate"><span class="pre">x</span></code> when creating a class instance will share the same copy of <code class="docutils literal notranslate"><span class="pre">x</span></code>. Because dataclasses just use normal Python class creation they also share this behavior. There is no general way for Data Classes to detect this condition. Instead, the <a class="reference internal" href="#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> decorator will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if it detects a default parameter of type <code class="docutils literal notranslate"><span class="pre">list</span></code>, <code class="docutils literal notranslate"><span class="pre">dict</span></code>, or <code class="docutils literal notranslate"><span class="pre">set</span></code>. This is a partial solution, but it does protect against many common errors.</p> <p>Using default factory functions is a way to create new instances of mutable types as default values for fields:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">D</span><span class="p">:</span> <span class="n">x</span><span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span> <span class="k">assert</span> <span class="n">D</span><span class="p">()</span><span class="o">.</span><span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">D</span><span class="p">()</span><span class="o">.</span><span class="n">x</span> </pre></div> </div> </div></blockquote> </section> <section id="descriptor-typed-fields"> <h2>Descriptor-typed fields<a class="headerlink" href="#descriptor-typed-fields" title="Permalink to this headline">¶</a></h2> <p>Fields that are assigned <a class="reference internal" href="../reference/datamodel.html#descriptors"><span class="std std-ref">descriptor objects</span></a> as their default value have the following special behaviors:</p> <ul class="simple"> <li><p>The value for the field passed to the dataclass’s <code class="docutils literal notranslate"><span class="pre">__init__</span></code> method is passed to the descriptor’s <code class="docutils literal notranslate"><span class="pre">__set__</span></code> method rather than overwriting the descriptor object.</p></li> <li><p>Similarly, when getting or setting the field, the descriptor’s <code class="docutils literal notranslate"><span class="pre">__get__</span></code> or <code class="docutils literal notranslate"><span class="pre">__set__</span></code> method is called rather than returning or overwriting the descriptor object.</p></li> <li><p>To determine whether a field contains a default value, <code class="docutils literal notranslate"><span class="pre">dataclasses</span></code> will call the descriptor’s <code class="docutils literal notranslate"><span class="pre">__get__</span></code> method using its class access form (i.e. <code class="docutils literal notranslate"><span class="pre">descriptor.__get__(obj=None,</span> <span class="pre">type=cls)</span></code>. If the descriptor returns a value in this case, it will be used as the field’s default. On the other hand, if the descriptor raises <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> in this situation, no default value will be provided for the field.</p></li> </ul> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">IntConversionDescriptor</span><span class="p">:</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">default</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_default</span> <span class="o">=</span> <span class="n">default</span> <span class="k">def</span> <span class="nf">__set_name__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">owner</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="s2">"_"</span> <span class="o">+</span> <span class="n">name</span> <span class="k">def</span> <span class="fm">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span> <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_default</span> <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_default</span><span class="p">)</span> <span class="k">def</span> <span class="fm">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="nd">@dataclass</span> <span class="k">class</span> <span class="nc">InventoryItem</span><span class="p">:</span> <span class="n">quantity_on_hand</span><span class="p">:</span> <span class="n">IntConversionDescriptor</span> <span class="o">=</span> <span class="n">IntConversionDescriptor</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span> <span class="n">i</span> <span class="o">=</span> <span class="n">InventoryItem</span><span class="p">()</span> <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">quantity_on_hand</span><span class="p">)</span> <span class="c1"># 100</span> <span class="n">i</span><span class="o">.</span><span class="n">quantity_on_hand</span> <span class="o">=</span> <span class="mf">2.5</span> <span class="c1"># calls __set__ with 2.5</span> <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">quantity_on_hand</span><span class="p">)</span> <span class="c1"># 2</span> </pre></div> </div> <p>Note that if a field is annotated with a descriptor type, but is not assigned a descriptor object as its default value, the field will act like a normal field.</p> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dataclasses</span></code> — Data Classes</a><ul> <li><a class="reference internal" href="#module-contents">Module contents</a></li> <li><a class="reference internal" href="#post-init-processing">Post-init processing</a></li> <li><a class="reference internal" href="#class-variables">Class variables</a></li> <li><a class="reference internal" href="#init-only-variables">Init-only variables</a></li> <li><a class="reference internal" href="#frozen-instances">Frozen instances</a></li> <li><a class="reference internal" href="#inheritance">Inheritance</a></li> <li><a class="reference internal" href="#re-ordering-of-keyword-only-parameters-in-init">Re-ordering of keyword-only parameters in <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a></li> <li><a class="reference internal" href="#default-factory-functions">Default factory functions</a></li> <li><a class="reference internal" href="#mutable-default-values">Mutable default values</a></li> <li><a class="reference internal" href="#descriptor-typed-fields">Descriptor-typed fields</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="warnings.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code> — Warning control</a></p> <h4>Next topic</h4> <p class="topless"><a href="contextlib.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">contextlib</span></code> — Utilities for <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code>-statement contexts</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/dataclasses.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="contextlib.html" title="contextlib — Utilities for with-statement contexts" >next</a> |</li> <li class="right" > <a href="warnings.html" title="warnings — Warning control" >previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">dataclasses</span></code> — Data Classes</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2026, Python Software Foundation. <br /> This page is licensed under the Python Software Foundation License Version 2. <br /> Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. <br /> See <a href="/license.html">History and License</a> for more information.<br /> <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> <br /> Last updated on January 26, 2026. <a href="/bugs.html">Found a bug</a>? <br /> Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2. </div> </body> </html>
Close