Skip to main content
1 vote
3 answers
54 views

I'm trying to subclass the AbstractWrappedSolver dataclass from the jax library diffrax. The class has this definition: class AbstractWrappedSolver(AbstractSolver[_SolverState]): """...
Ben's user avatar
  • 603
Advice
0 votes
2 replies
97 views

I'm learning for my Introductory Programming Course in uni and I don't have an overview over the different cases when it comes to Setters and Getters and the slides are not quite helpful. So for ...
alex thyo's user avatar
7 votes
1 answer
170 views

I'm currently working on a library that uses dataclasses.dataclass classes for data structures. I'm utilizing the metadata argument of the dataclasses.field() method to inject custom library ...
JackTheFoxOtter's user avatar
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
1 vote
1 answer
71 views

Why can't I simply define a 'derived' variable in the class definition and assign it a calculated value, instead of using the __post_init__ method to do so? Example: from dataclasses import dataclass, ...
Purav Dagli's user avatar
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
Best practices
2 votes
3 replies
136 views

I find Python's dataclasses to be incredibly convenient. They cut out a ton of boilerplate code (no more needing to write self.foo = foo for every __init__ parameter, and no more needing to spell out ...
Lux's user avatar
  • 352
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
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
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
-2 votes
1 answer
73 views

import dataclasses @dataclasses.dataclass class MyClass: a: int b: int = dataclasses.field(default=0, init=False) m = MyClass(a=0) print(repr(m)) # prints: "MyClass(a=0, b=0)" # `...
Guti_Haz's user avatar
  • 2,752
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
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
2 votes
2 answers
242 views

I have this data class: @dataclass class User: id: str name: str pwd: str picture: str url: str age: int # many, many more attributes Now, I have to make all of the ...
Aadvik's user avatar
  • 1,522
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

15 30 50 per page
1
2 3 4 5
70