Skip to main content

A backport of the dataclasses module for Python 3.6

Project description

https://img.shields.io/pypi/v/dataclasses.svg

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_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 sort 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.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

dataclasses-0.6.tar.gz (36.8 kB view details)

Uploaded Source

Built Distribution

dataclasses-0.6-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file dataclasses-0.6.tar.gz.

File metadata

  • Download URL: dataclasses-0.6.tar.gz
  • Upload date:
  • Size: 36.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dataclasses-0.6.tar.gz
Algorithm Hash digest
SHA256 6988bd2b895eef432d562370bb707d540f32f7360ab13da45340101bc2307d84
MD5 127fcfe8c4a99d2968e31efdb0362ed7
BLAKE2b-256 59e42f921edfdf1493bdc07b914cbea43bc334996df4841a34523baf73d1fb4f

See more details on using hashes here.

File details

Details for the file dataclasses-0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclasses-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 454a69d788c7fda44efd71e259be79577822f5e3f53f029a22d08004e951dc9f
MD5 cc987fdfba4e1c07cee4f6919055ba6e
BLAKE2b-256 262f1095cdc2868052dd1e64520f7c0d5c8c550ad297e944e641dbf1ffbb9a5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page