Python Forum
append str to list in dataclass
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append str to list in dataclass
#1
Dear community,

I'm trying to append str to a list, which is in a dataclass.
I'm totally new to dataclasses.
I used to use a textfile for storing strings.

Then I read about dataclasses for storing data.

What is the simplest way for appending string to a list in a dataclass?

When I try mylist: list = []
then it is said to use default_factory...

@dataclass
class C():
    mylist: list
    a: str

    def __post_init__(self):
        return self.mylist.append(self.a)


c = C("a")
print(c.mylist)
c = C("b")
print(c.mylist)
mylist should be ["a", "b"].

Thanks a lot for the help, I did a lot of googling, but with no success...
Reply


Messages In This Thread
append str to list in dataclass - by flash77 - Mar-14-2024, 03:41 PM
RE: append str to list in dataclass - by deanhystad - Mar-14-2024, 03:58 PM
RE: append str to list in dataclass - by flash77 - Mar-14-2024, 04:22 PM
RE: append str to list in dataclass - by deanhystad - Mar-14-2024, 04:41 PM
RE: append str to list in dataclass - by flash77 - Mar-14-2024, 04:56 PM
RE: append str to list in dataclass - by snippsat - Mar-14-2024, 05:25 PM
RE: append str to list in dataclass - by flash77 - Mar-14-2024, 06:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to append integers from file to list? Milan 8 1,542 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  read a text file, find all integers, append to list oldtrafford 12 3,807 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 11,045 Jun-12-2022, 06:54 PM
Last Post: Mark17
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,386 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to append to list a function output? rama27 5 6,865 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 2,460 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Append only adding the same list again and again viraj1123 4 2,099 Jun-17-2020, 07:26 AM
Last Post: viraj1123
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,654 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Problem with append list in loop michaelko03 0 1,715 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  append list to empty array SchroedingersLion 1 2,234 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020