Skip to main content
1966 votes
17 answers
1.4m views

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 ...
Nelson's user avatar
  • 30.2k
508 votes
5 answers
298k views

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 ...
kingJulian's user avatar
  • 6,340
345 votes
18 answers
256k views

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 ...
Mysterio's user avatar
  • 3,586
307 votes
8 answers
133k views

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 ...
Oleh Rybalchenko's user avatar
272 votes
4 answers
196k views

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 ...
Graham's user avatar
  • 4,061
271 votes
3 answers
135k views

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 ...
Pavel Hanpari's user avatar
217 votes
14 answers
233k views

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 >>&...
miracle2k's user avatar
  • 32.7k
212 votes
18 answers
217k views

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: ...
mbatchkarov's user avatar
  • 16.2k
186 votes
4 answers
112k views

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(...
Brian C.'s user avatar
  • 8,240
172 votes
10 answers
17k views

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 ...
MWB's user avatar
  • 12.8k
147 votes
2 answers
208k views

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 =...
H.Bukhari's user avatar
  • 2,411
112 votes
20 answers
170k views

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 ...
GertVdE's user avatar
  • 1,443
107 votes
1 answer
113k views

What's the difference between @dataclass(frozen=True) and @dataclass(frozen=False)? When should I use which?
Fl4ggi LP's user avatar
  • 1,081
103 votes
4 answers
95k views

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 ...
Arne's user avatar
  • 20.8k
98 votes
2 answers
59k views

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: ...
Kurt Kline's user avatar
  • 2,159

15 30 50 per page
1
2 3 4 5
70