Skip to main content
  • dataclass: like dataclasses.dataclass, except:
    • Adds three new instance methods to each dataclass
      • asdict(), astuple(), replace()

    • …and one new class method,
      • fields()

    • frozen=True is now the default!

    • xmod -ed for less cruft

  • dataclass.field: Like dataclasses.field, except:
    • default_factory is now a positional parameter

    • perfectly backward compatible

Usage examples

import dataclass
from typing import Dict

@dataclass
class One:
    one: str = 'one'
    two: int = 2
    three: Dict = dataclass.field(dict)  # Simplified `field`

#
# Three new instance methods
#
o = One()
assert o.asdict() == {'one': 'one', 'two': 2, 'three': {}}
assert o.astuple() == ('one', 2, {})

o2 = o.replace(one='seven', three={'nine': 9})
assert o2 == One('seven', 2, {'nine': 9})

#
# A new class method
#
assert [f.name for f in One.fields()] == ['one', 'two', 'three']

#
# Immutable by default
#
try:
    o.one = 'three'
except AttributeError:
    pass
else:
    raise AttributeError('Was mutable!')

@dataclass(frozen=False)
class OneMutable:
    one: str = 'one'
    two: int = 2
    three: Dict = dataclass.field(dict)

om = OneMutable()
om.one = 'three'
assert str(om) == "OneMutable(one='three', two=2, three={})"

#
# These four new methods won't break your old dataclasses:
#
@dataclass
class Overloads:
    one: str = 'one'
    asdict: int = 1
    astuple: int = 1
    fields: int = 1
    replace: int = 1

o = Overloads()
assert ov.one == 'one'
assert ov.asdict == 1
assert ov.astuple == 1
assert ov.fields == 1
assert ov.replace == 1

# In this case, you can access them functions on dataclass:
assert (
    dataclass.asdict(ov) ==
    {'asdict': 1, 'astuple': 1, 'fields': 1, 'one': 'one', 'replace': 1}
)

assert dataclass.astuple(ov) == ('one', 1, 1, 1, 1)

Metadata

Release files for dataclas 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataclas 0.9.3
File Size Uploaded
dataclas-0.9.3.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dataclas 0.9.3
File Interpreter ABI Platform
dataclas-0.9.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.1 kB

Release files / dataclas-0.9.3.tar.gz

Download URL dataclas-0.9.3.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0289d75a72a2f6f72c50312091e36fa20d82af055b886f55d293f799622f586f
BLAKE2b-256 checksum
How to use checksums
5c3fb1d26a2fa4980a4e64e8083af956bb26aa4dd75ece935823ccf3efc47111
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.0

Release files / dataclas-0.9.3-py3-none-any.whl

Download URL dataclas-0.9.3-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c311bad52eafe631f15372d6bf9f420ed8aa9b4a2ded6b951d3482f7c32f8e68
BLAKE2b-256 checksum
How to use checksums
1e83d3e1b2f6b92646682c37eb1c0d0d28b82bc1309218f436cc4bab4834da70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.9.3 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page