python immutable dataclass 1

python immutable dataclass

# truly immutable Python objects are not possible, 
# but it's simulated by throwing an exception if you want to set fields
from dataclasses import dataclass

@dataclass(frozen=True)
class MyImmutableData:
    immutable_field: str

Here is what the above code is Doing:
1. The @dataclass decorator is used to create a class that has a bunch of fields.
2. The frozen=True argument tells Python to make the class immutable.
3. The immutable_field field is a string.

Similar Posts