'Python dataclass with list
So just learnng Python 3.7 and I want to create a simple dataclass with two data members, an int and a list somethng like :
class myobject:
data1:int
data2:List[]
object1=myobject(1, 1)
myobject.data2.append(5)
etc...
I tried quite a few things but apparently the List is seen as an integer only not a list and I don't know what to do, can somebody helping me please?
cheers
Solution 1:[1]
As noted in comments, the type hints are not enforced by Python. If you pass an int where you specified a list, you will get an int.
You are also creating an object of the myobject class, but then not using it. I think you want something like:
from dataclasses import dataclass
@dataclass
class myobject:
data1: int
data2: list
object1 = myobject(1, [1])
object1.data2.append(5)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
