I am using dataclass and I would like to create a time stamp every time an instance of the class is created, like so:
@dataclass
class test:
timestamp: datetime = datetime.utcnow()
test1 = test()
However, doing this, yields a timestamp as of the time when I ran the code for original class object. That is, new instances all have the same timestamp.
The approach below does what I want, but I don't understand why. I would think that the above approach should work as well, since there is no instance of that class created. Why do I have to write it with that field approach?
@dataclass
class test:
timestamp: datetime = field(default_factory=datetime.utcnow)
test1 = test()
test1.timestamp