Questions tagged [python-dataclasses]

For questions concerning the Python dataclasses module (new in Python 3.7). Dataclasses are python classes but are specifically suited for storing data objects.

Dataclasses are python classes but are specifically suited for storing data objects(store data and represent a certain data type).

Official documentation: dataclasses

Original proposal: PEP 557

Tutorial / showcase: Raymond Hettinger's talk on PyCon 2018

899 questions
443
votes
4 answers

What are data classes and how are they different from common classes?

With PEP 557 data classes are introduced into python standard library. They make use of the @dataclass decorator and they are supposed to be "mutable namedtuples with default" but I'm not really sure I understand what this actually means and how…
kingJulian
  • 5,601
  • 5
  • 17
  • 30
274
votes
16 answers

Class inheritance in Python 3.7 dataclasses

I'm currently trying my hands on the new dataclass constructions introduced in Python 3.7. I am currently stuck on trying to do some inheritance of a parent class. It looks like the order of the arguments are botched by my current approach such that…
Mysterio
  • 2,878
  • 2
  • 15
  • 21
235
votes
3 answers

Type hints in namedtuple

Consider following piece of code: from collections import namedtuple point = namedtuple("Point", ("x:int", "y:int")) The Code above is just a way to demonstrate as to what I am trying to achieve. I would like to make namedtuple with type hints. Do…
232
votes
7 answers

Data Classes vs typing.NamedTuple primary use cases

Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple. And now I'm wondering how to separate the use cases in which namedtuple is still a…
Oleh Rybalchenko
  • 6,998
  • 3
  • 22
  • 36
173
votes
17 answers

Python dataclass from a nested dict

The standard library in 3.7 can recursively convert a dataclass into a dict (example from the docs): from dataclasses import dataclass, asdict from typing import List @dataclass class Point: x: int y: int @dataclass class C: mylist:…
mbatchkarov
  • 15,487
  • 9
  • 60
  • 79
173
votes
13 answers

Make the Python json encoder support Python's new dataclasses

Starting with Python 3.7, there is something called a dataclass: from dataclasses import dataclass @dataclass class Foo: x: str However, the following fails: >>> import json >>> foo = Foo(x="bar") >>> json.dumps(foo) TypeError: Object of type…
miracle2k
  • 29,597
  • 21
  • 65
  • 64
172
votes
10 answers

How do I avoid the "self.x = x; self.y = y; self.z = z" pattern in __init__?

I see patterns like def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... quite frequently, often with a lot more parameters. Is there a good way to avoid this type of tedious repetitiveness? Should the class…
MWB
  • 11,740
  • 6
  • 46
  • 91
139
votes
3 answers

How can I make a python dataclass hashable?

Say a I have a dataclass in python3. I want to be able to hash and order these objects. I only want them ordered/hashed on id. I see in the docs that I can just implement _hash_ and all that but I'd like to get datacalsses to do the work for me…
Brian C.
  • 6,455
  • 3
  • 32
  • 42
103
votes
2 answers

Passing default list argument to dataclasses

I would like to pass default argument in my class, but somehow I am having problem: from dataclasses import dataclass, field from typing import List @dataclass class Pizza(): ingredients: List = field(default_factory=['dow', 'tomatoes']) …
H.Bukhari
  • 1,951
  • 3
  • 10
  • 15
85
votes
4 answers

Validating detailed types in python dataclasses

Python 3.7 was released a while ago, and I wanted to test some of the fancy new dataclass+typing features. Getting hints to work right is easy enough, with both native types and those from the typing module: >>> import dataclasses >>> import typing…
Arne
  • 17,706
  • 5
  • 83
  • 99
84
votes
18 answers

Dataclasses and property decorator

I've been reading up on Python 3.7's dataclass as an alternative to namedtuples (what I typically use when having to group data in a structure). I was wondering if dataclass is compatible with the property decorator to define getter and setter…
GertVdE
  • 1,153
  • 1
  • 7
  • 12
81
votes
3 answers

How can I get Python 3.7 new dataclass field types?

Python 3.7 introduces new feature called data classes. from dataclasses import dataclass @dataclass class MyClass: id: int = 0 name: str = '' When using type hints (annotation) in function parameters, you can easily get annotated types…
Kamyar
  • 2,494
  • 2
  • 22
  • 33
68
votes
1 answer

What does frozen mean for dataclasses?

What's the difference between @dataclass(frozen=True) and @dataclass(frozen=False)? When should I use which?
Fl4ggi LP
  • 701
  • 1
  • 4
  • 6
66
votes
2 answers

Proper way to create class variable in Data Class

I've just begun playing around with Python's Data Classes, and I would like confirm that I am declaring Class Variables in the proper way. Using regular python classes class Employee: raise_amount = .05 def __init__(self, fname, lname,…
Kurt Kline
  • 1,724
  • 1
  • 10
  • 23
66
votes
5 answers

Easiest way to copy all fields from one dataclass instance to another?

Let's assume you have defined a Python dataclass: @dataclass class Marker: a: float b: float = 1.0 What's the easiest way to copy the values from an instance marker_a to another instance marker_b? Here's an example of what I try to…
Tom Pohl
  • 2,711
  • 3
  • 27
  • 34
1
2 3
59 60