python-dataclasses

How to add a property to a child dataclass before a default property?

How to add a property to a child dataclass before a default property? Question: Let’s suppose that I have a dataclass A. The dataclass A contains some property with a default value. Let’s suppose that I want to extend that dataclass with a dataclass B. I need to add some non-default property to the dataclass …

Total answers: 1

Make Python dataclass iterable?

Make Python dataclass iterable? Question: I have a dataclass and I want to iterate over in in a loop to spit out each of the values. I’m able to write a very short __iter__() within it easy enough, but is that what I should be doing? I don’t see anything in the documentation about an …

Total answers: 2

Store the order of arguments given to dataclass initializer

Store the order of arguments given to dataclass initializer Question: Using the Python dataclass decorator generates signatures with arguments in a particular order: from dataclasses import dataclass from inspect import signature @dataclass class Person: age: int name: str = ‘John’ print(signature(Person)) Gives (age: int, name: str = ‘John’) -> None. Is there a way to …

Total answers: 2

python data class default value for str to None

python data class default value for str to None Question: I have a dataclass like this: from dataclasses import dataclass @dataclass class DataClassCard: rank: str = None suit: str I am getting an error saying: TypeError: non-default argument ‘suit’ follows default argument Is there anyway to set this default value? Finally calling: queen_of_hearts = DataClassCard(suit …

Total answers: 1

Python: how to type hint a dataclass?

Python: how to type hint a dataclass? Question: The code below works, but I’m getting the following warning by PyCharm: Cannot find reference __annotations__ in ‘(…) -> Any’. I guess it’s because I’m using Callable. I didn’t find something like Dataclass. Which type should I use instead? from __future__ import annotations from dataclasses import dataclass …

Total answers: 3

Recursively changing values of a nested dataclass in python

Recursively changing values of a nested dataclass in python Question: I have a dataclass like this: class chapter: title: str text: str = ” chapter: List[‘chapter’] = field(default_factory=list) removed: bool = False Say there is a list object that contains instances of that dataclass with these values: content = [ chapter( ‘chapter 1’, chapter=[ chapter(‘subchapter …

Total answers: 2

How to get the index of a dataclass field

How to get the index of a dataclass field Question: Say I have a simple dataclass instance import dataclasses as dc @dc.dataclass class DCItem: name: str unit_price: float item = DCItem(‘test’, 11) Now I want to determine the position (index) of instance attribute item.unit_price. How can I make it simple to use and performant? I …

Total answers: 1

dataclass requiring a value after being defined as Optional

dataclass requiring a value after being defined as Optional Question: In the snippet below, I can’t understand why I need to explicitly provide a value for the TimeCollection. How do I set this up so the line becomes plan1 = TimeCollection() from __future__ import annotations from dataclasses import dataclass import datetime from typing import Optional, …

Total answers: 1