1,040 questions
1966
votes
17
answers
1.4m
views
How do I declare custom exceptions in modern Python?
How do I declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I include in the ...
508
votes
5
answers
298k
views
What are data classes and how are they different from common classes?
PEP 557 introduces data classes into the Python standard library. It says that by applying the @dataclass decorator shown below, it will generate "among other things, an __init__()".
from ...
345
votes
18
answers
256k
views
Overriding default values when using inheritance in dataclasses
I'm currently trying my hands on the dataclass constructions. 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 ...
307
votes
8
answers
133k
views
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 ...
272
votes
4
answers
196k
views
Why can't dataclasses have mutable defaults in their class attributes declaration?
This is similar to Passing default list argument to dataclasses, but it's not quite what I'm looking for.
Here's the problem: when one tries to assign a mutable value to a class attribute, there's an ...
271
votes
3
answers
135k
views
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 ...
217
votes
14
answers
233k
views
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
>>&...
212
votes
18
answers
217k
views
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: ...
186
votes
4
answers
112k
views
How can I make a python dataclass hashable?
I have a dataclass whose instances I want to hash and order, using the id member as a key.
from dataclasses import dataclass, field
@dataclass(eq=True, order=True)
class Category:
id: str = field(...
172
votes
10
answers
17k
views
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 ...
147
votes
2
answers
208k
views
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 =...
112
votes
20
answers
170k
views
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 ...
107
votes
1
answer
113k
views
What does frozen mean for dataclasses?
What's the difference between @dataclass(frozen=True) and @dataclass(frozen=False)? When should I use which?
103
votes
4
answers
95k
views
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 ...
98
votes
2
answers
59k
views
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:
...