Questions tagged [python-dataclasses]

For questions concerning the Python dataclasses module (new in Python 3.7). Dataclasses are python classes but are specifically suited for storing data objects.

Dataclasses are python classes but are specifically suited for storing data objects(store data and represent a certain data type).

Official documentation: dataclasses

Original proposal: PEP 557

Tutorial / showcase: Raymond Hettinger's talk on PyCon 2018

899 questions
-1
votes
1 answer

Remove dataclass dependency

I am using Python 3.5 which does not have support for dataclass. Is there a way to convert the following class to work without dataclasses? from dataclasses import dataclass @dataclass class Cookie: """Models a cookie.""" domain: str …
Bijan
  • 7,737
  • 18
  • 89
  • 149
-1
votes
1 answer

Using Dataclass and getting AttributeError: 'int' object has no attribute 'x'

Trying out the code from. from dataclasses import dataclass, field, InitVar @dataclass class XYPoint: last_serial_no = 0 x: float y: float = 0 skip: InitVar[int] = 1 serial_no: int = field(init=False) def…
tbc
  • 49
  • 1
  • 6
-1
votes
1 answer

Dataclass: NameError: name 'WORD_TYPE' is not defined

I tried to work with SO question example @dataclass class WordItem: id: str = field(init=False) phrase: str word_type: WORD_TYPE def __post_init__(self): self.id = f'{self.phrase}_{self.word_type.name.lower()}' I…
MikiBelavista
  • 2,470
  • 10
  • 48
  • 70
-2
votes
0 answers

Python dataclass __post_init__() method is being called twice

import dataclasses @dataclasses.dataclass class FlexibilityResult: lengths: list[float] breadths: list[float] heights: list[float] volumes: list[float] = dataclasses.field(default_factory=lambda: []) def __post_init__(self): …
-2
votes
2 answers

function from_dict() failing for unknown reason in python

I converted below JSON using https://json2csharp.com/code-converters/json-to-python to a dataclass: { "bypassCd": [ "Duis sint ipsum in", "consequat" ] } It generaged below dataclass - for some reason, it is…
jimsweb
  • 1,082
  • 2
  • 17
  • 37
-2
votes
1 answer

Python dataclass: Forcing a dictionary field to be a deep copy

I am working with a dataclass that contains a dict. I want the dict to be a deepcopy, without having to rely on a post_init call, which would basically void to interest of a dataclass What would be a nice solution ? from dataclasses import…
Milan
  • 1,547
  • 1
  • 24
  • 47
-2
votes
3 answers

Change class into dataclass

I want to refactor a class into a dataclass, here is what I have: class Tenant: def __init__(self, tenant: dict, api_client: str) -> None: self.name = tenant["name"] self.tenant_id= tenant["id"] self.subdomain =…
ganjin
  • 11
  • 1
-2
votes
1 answer

Create an empty object of a specified type dynamically

Using a dataclass to specify a set of environment variables and am trying to keep the dataclass separate from a class that modifies the environment. Passing in an instance of the dataclass and have a get_env that determines if those variables are…
-2
votes
1 answer

How I can implement *asdict() or *asdict() using @dataclass

I can't implement asdisct. I have read information but I still have errors when using asdict. @dataclass class InfoMessage(): """Информационное сообщение o тренировке.""" def __init__(self, training_type: str, # Тип…
-2
votes
3 answers

representing a dataclass as a dictionary/JSON in python without calling a method

I have, for example, this class: from dataclasses import dataclass @dataclass class Example: name: str = "Hello" size: int = 10 I want to be able to return a dictionary of this class without calling a to_dict function, dict or…
-3
votes
2 answers

How to Access a variable inside A dataclass In python?

I'm trying to write A dataclass for my python program , My idea was to use the dataclass as the full database , But I do not know how to access the variable from a dataclass. I'd be glad if Someone would be able to point me in the right…
KhodeNima
  • 1
  • 3
-3
votes
1 answer

What type of 'field' for a time value

What field type would you use in a dataclass where the value provided is in a time format? The JSON file I'm parsing returns a value like this: 2.00:00:15.0830000 In a class like this below, what type should I set? from dataclasses import…
elcade
  • 1
  • 3
-3
votes
1 answer

Dataclasses for dataframes in the class

I have a Result class in which I calculate many dataframes (around 30) and assign them to attributes of the class. class Result: def __init__(self, df1) self.df1=df1 I want to write the dataframes to excel at specified positions. For this…
Ryszard Eggink
  • 173
  • 1
  • 9
-3
votes
1 answer

Why does a Python dataclass "persist" outside the scope of its defining function?

Consider the following minimal example: >>> from dataclasses import dataclass >>> @dataclass ... class my_stuff: ... my_variable: int = 0 ... >>> def my_func(): ... stuff = my_stuff ... stuff.my_variable += 1 ... …
1 2 3
59
60