1,040 questions
1
vote
3
answers
54
views
Assigning abstract attributes through the constructor in a `jax` python dataclass
I'm trying to subclass the AbstractWrappedSolver dataclass from the jax library diffrax. The class has this definition:
class AbstractWrappedSolver(AbstractSolver[_SolverState]):
"""...
Advice
0
votes
2
replies
97
views
Getters and Setters in Python (Dataclasses)
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 ...
7
votes
1
answer
170
views
How to annotate a function that returns a dataclass field so that type checkers treat it correctly?
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 ...
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 ...
1
vote
1
answer
71
views
Dataclasses __post_init__ method
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, ...
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 ...
Best practices
2
votes
3
replies
136
views
What signs indicate that Python's dataclasses are NOT a good fit for a particular use-case?
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 ...
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: ...
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 ...
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
>>&...
-2
votes
1
answer
73
views
dataclass __repr__ shows init=False members
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)"
# `...
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 ...
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 ...
2
votes
2
answers
242
views
How can I make all attributes in a Python dataclass optional without rewriting each one?
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 ...
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 ...