A backport of the dataclasses module for Python 3.6
Project description
This is an implementation of PEP 557, Data Classes. It is a backport for Python 3.6. Because dataclasses will be included in Python 3.7, any discussion of dataclass features should occur on the python-dev mailing list at https://mail.python.org/mailman/listinfo/python-dev. At this point this repo should only be used for historical purposes (it’s where the original dataclasses discussions took place) and for discussion of the actual backport to Python 3.6.
See https://www.python.org/dev/peps/pep-0557/ for the details of how Data Classes work.
A test file can be found at https://github.com/ericvsmith/dataclasses/blob/master/test/test_dataclasses.py, or in the sdist file.
Installation
pip install dataclasses
Example Usage
from dataclasses import dataclass
@dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand
item = InventoryItem('hammers', 10.49, 12)
print(item.total_cost())
Some additional tools can be found in dataclass_tools.py, included in the sdist.
Compatibility
This backport assumes that dict objects retain their insertion order. This is true in the language spec for Python 3.7 and greater. Since this is a backport to Python 3.6, it raises an interesting question: does that guarantee apply to 3.6? For CPython 3.6 it does. As of the time of this writing, it’s also true for all other Python implementations that claim to be 3.6 compatible, of which there are none. Any new 3.6 implementations are expected to have ordered dicts. See the analysis at the end of this email:
https://mail.python.org/pipermail/python-dev/2017-December/151325.html
As of version 0.4, this code no longer works with Python 3.7. For 3.7, use the built-in dataclasses module.
Release History
Version |
Date |
Description |
---|---|---|
0.8 |
2020-11-13 |
Fix ClassVar in .replace() |
0.7 |
2019-10-20 |
Require python 3.6 only |
0.6 |
2018-05-17 |
Equivalent to Python 3.7.0rc1 |
0.5 |
2018-03-28 |
Equivalent to Python 3.7.0b3 |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dataclasses-0.8.tar.gz
.
File metadata
- Download URL: dataclasses-0.8.tar.gz
- Upload date:
- Size: 36.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.24.0 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8479067f342acf957dc82ec415d355ab5edb7e7646b90dc6e2fd1d96ad084c97 |
|
MD5 | c9a52e322ada1406bfde9ebe3cdbbcca |
|
BLAKE2b-256 | 1f127919c5d8b9c497f9180db15ea8ead6499812ea8264a6ae18766d93c59fe5 |
File details
Details for the file dataclasses-0.8-py3-none-any.whl
.
File metadata
- Download URL: dataclasses-0.8-py3-none-any.whl
- Upload date:
- Size: 19.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.24.0 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0201d89fa866f68c8ebd9d08ee6ff50c0b255f8ec63a71c16fda7af82bb887bf |
|
MD5 | 0d6e5bb7adcc8fb0de3ef1ace1b32999 |
|
BLAKE2b-256 | feca75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43 |