python-dataclasses Questions

7

Solved

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 s...
Blackguard asked 3/8, 2018 at 11:32

1

Solved

I have a dataclass with (kind of) a getter method. This code works as expected: from dataclasses import dataclass @dataclass() class A: def get_data(self): # get some values from object's fields...
Longe asked 4/8, 2023 at 13:29

4

Solved

Given a dataclass like below: class MessageHeader(BaseModel): message_id: uuid.UUID def dict(self, **kwargs): return json.loads(self.json()) I would like to get a dictionary of string literal ...
Buckhound asked 13/6, 2022 at 14:54

3

Solved

for some reason, I use the toml config instead of the .py config. then, the singleton nightmare comes, continuously come up with not init, or RecursionError is there an elegant way to use a python ...
Locular asked 30/6, 2023 at 2:30

17

Solved

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: i...
Hying asked 19/11, 2018 at 13:51

2

Suppose I have a dataclass with a set method. How do I extend the repr method so that it also updates whenever the set method is called: from dataclasses import dataclass @dataclass class State: A...
Reassure asked 30/4, 2021 at 2:3

7

Let's say I have a class like this: class C: def __init__(self, stuff: int): self._stuff = stuff @property def stuff(self) -> int: return self._stuff then stuff is read-only: c = C(stuff...
Alesiaalessandra asked 15/4, 2020 at 19:32

4

Solved

What I kown The Python dataclass allows inheritance, either with dataclass or class. In best practice (and also in other languages), when we do inheritance, the initialization should be called firs...
Nigercongo asked 28/2, 2019 at 14:19

3

I have a fixed set of three sensors that I want to model as an enum. Each of these sensors is parametrised by a few different attributes. I therefore want to model the sensors themselves as a datac...
Belindabelisarius asked 31/1, 2022 at 10:17

2

I want to parse json and save it in dataclasses to emulate DTO. Currently, I ahve to manually pass all the json fields to dataclass. I wanted to know is there a way I can do it by just adding the j...
Billboard asked 26/4, 2022 at 11:46

3

Solved

I'm trying to use the new python dataclasses to create some mix-in classes (already as I write this I think it sounds like a rash idea), and I'm having some issues. Behold the example below: from d...

5

Solved

I'm trying to use queue.PriorityQueue in Python 3(.6). I would like to store objects with a given priority. But if two objects have the same priority, I don't mind PriorityQueue.get to return eith...
Weeds asked 3/1, 2019 at 18:28

1

Solved

Is there a way to check if a Python dataclass has been set to frozen? If not, would it be valuable to have a method like is_frozen in the dataclasses module to perform this check? e.g. from datacla...
Porphyry asked 7/4, 2023 at 12:5

18

Solved

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 ...
Osculation asked 28/6, 2018 at 9:37

5

Solved

I'd like to replace the attributes of a dataclass instance, analogous to namedtuple._replace(), i.e. making an altered copy of the original object: from dataclasses import dataclass from collectio...
Infirmary asked 18/4, 2018 at 20:21

12

Solved

I have a dataclass object that has nested dataclass objects in it. However, when I create the main object, the nested objects turn into a dictionary: @dataclass class One: f_one: int f_two: str ...
Costanzo asked 27/7, 2018 at 20:3

3

Solved

I have a json string that I want to read, convert it to an object that I can manipulate, and then convert it back into a json string. I am utilizing the python 3.10 dataclass, and one of the attrib...
Duala asked 15/9, 2022 at 18:38

2

I was just playing around with the concept of Python dataclasses and abstract classes and what i am trying to achieve is basically create a frozen dataclass but at the same time have one attribute ...
Ozoniferous asked 6/12, 2019 at 23:56

8

Solved

Currently I used DTO(Data Transfer Object) like this. class Test1: def __init__(self, user_id: int = None, body: str = None): self.user_id = user_id self.body = body Example code is very s...
Noemi asked 11/3, 2019 at 9:56

3

Solved

Note this is similar to How to get @property methods in asdict?. I have a (frozen) nested data structure like the following. A few properties that are (purely) dependent on the fields are defined. ...

6

I am using a dataclass and field to pass in a default value. When an argument is provided I want to validate it using a descriptor class. Is there any way to utilize the benefits of field (repr, de...
Leroylerwick asked 20/5, 2021 at 0:21

4

Solved

I want to know a simple way to make a dataclass bar frozen. @dataclass class Bar: foo: int bar = Bar(foo=1) In other words, I want a function like the following some_fn_to_freeze frozen_bar = som...
Intima asked 10/5, 2019 at 0:53

2

Solved

If you don't add any more fields to your subclass is there a need to add the @dataclass decorator to it and would it do anything? If there is no difference, which is the usual convention? from data...
Morly asked 24/11, 2022 at 16:6

2

Solved

I stumbled upon a problem, when I was working on my ETL pipeline. I am using dataclasses dataclass to parse JSON objects. One of the keywords of the JSON object is a reserved keyword. Is there a wa...
Spent asked 5/2, 2020 at 11:2

9

Solved

I need a class that will accept a number of parameters, I know that all parameters will be provided but some maybe passed as None in which case my class will have to provide default values. I want ...
Acerbic asked 19/6, 2019 at 10:14

© 2022 - 2025 — McMap. All rights reserved.