I have a dataclass that looks like this (simplified and renamed):
@dataclass
class SharedReference:
bytes_io: BytesIO = BytesIO()
And I am running into an issue where I'm accidentally creating a shared reference so closing the IO stream for one instance closes it for all.
Looking at the ids, I see it is referencing the same memory id:
shared = SharedReference()
print(shared.bytes_io) # 0x7f...5e0
shared.bytes_io.close()
shared_2 = SharedReference()
print(shared_2.bytes_io) # 0x7f...5e0 (same id)
print(shared_2.bytes_io.closed) # True (got accidentally closed)
But this dataclass:
@dataclass
class SeparateReference:
bytes_io: BytesIO = field(init=False)
def __post_init__(self):
self.bytes_io = BytesIO()
works properly:
separate = SeparateReference()
print(separate.bytes_io) # 0x7f...d10
separate.bytes_io.close()
separate = SeparateReference()
print(separate.bytes_io) # 0x7f...680 (different id)
print(separate.bytes_io.closed) # False (didn't get accidentally closed)
Why does the second work but not the first?