in Education by
How do I pickle an instance of a frozen dataclass with __slots__? For example, the following code raises an exception in Python 3.7.0: import pickle from dataclasses import dataclass @dataclass(frozen=True) class A: __slots__ = ('a',) a: int b = pickle.dumps(A(5)) pickle.loads(b) Traceback (most recent call last): File "", line 1, in File "", line 3, in __setattr__ dataclasses.FrozenInstanceError: cannot assign to field 'a' This works if I remove either the frozen or the __slots__. Is this just a bug? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The problem comes from pickle using the __setattr__ method of the instance when setting the state of the slots. The default __setstate__ is defined in load_build in _pickle.c line 6220. For the items in the state dict, the instance __dict__ is updated directly: if (PyObject_SetItem(dict, d_key, d_value) < 0) whereas for the items in the slotstate dict, the instance's __setattr__ is used: if (PyObject_SetAttr(inst, d_key, d_value) < 0) Now because the instance is frozen, __setattr__ raises FrozenInstanceError when loading. To circumvent this, you can define your own __setstate__ method which will use object.__setattr__, and not the instance's __setattr__. The docs give some sort of warning for this: There is a tiny performance penalty when using frozen=True: __init__() cannot use simple assignment to initialize fields, and must use object.__setattr__(). It may also be good to define __getstate__ as the instance __dict__ is always None in your case. If you don't, the state argument of __setstate__ will be a tuple (None, {'a': 5}), the first value being the value of the instance's __dict__ and the second the slotstate dict. import pickle from dataclasses import dataclass @dataclass(frozen=True) class A: __slots__ = ('a',) a: int def __getstate__(self): return dict( (slot, getattr(self, slot)) for slot in self.__slots__ if hasattr(self, slot) ) def __setstate__(self, state): for slot, value in state.items(): object.__setattr__(self, slot, value) # <- use object.__setattr__ b = pickle.dumps(A(5)) pickle.loads(b) I personally would not call it a bug as the pickling process is designed to be flexible, but there is room for a feature enhancement. A revision of the pickling protocol could fix this in future. Unless I am missing something and aside of the tiny performance penalty, using PyObject_GenericSetattr for all the slots might be a reasonable fix?

Related questions

0 votes
    from datetime import date import dataclasses @dataclasses.dataclass(frozen=True) class A: date: date = dataclasses.field ... by design? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Sometimes, you may notice a black powder or white discs floating on the pickle or murabba, when a ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Salt is applied on the inner surface of pickle jars and the pickle is covered with oil. Why ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Following on from my question on using frozen Capistrano a couple of days back I'm still having issues ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Which of the following function convert a string to a frozen set in python? A - set(x) B - dict(d) C - frozenset(s) D - chr(x)...
asked Apr 10, 2021 in Education by JackTerrance
0 votes
    How will you convert a string to a frozen set in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    git bisect is used to identify a commit that has introduced a bug. A. True B. False...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    Which algorithm chooses the page that has not been used for the longest period of time whenever the page ... recently used algorithm d) counting based page replacement algorithm...
asked Oct 27, 2022 in Education by JackTerrance
0 votes
    Do you agree that Indian democracy has become profound? Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
0 votes
    Only one Indian has so far been the President of the UN General Assembly. Who is that person? When and at which session? Please answer the above question....
asked Aug 10, 2022 in Education by JackTerrance
0 votes
    Type of equality which says that every citizen has an equal right to participate in affairs of the State. Please answer the above question....
asked Aug 4, 2022 in Education by JackTerrance
0 votes
    (2) Do you agree that Indian democracy has become profound? Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I wish to retrieve my the elements inside Users that as a field of ArrayList and display each schedule ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Which of these class object has an architecture similar to that of array? (a) Bitset (b) Map (c) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I am using simple thread to execute the httpGet to server when a button is clicked, but I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
...