2

I have the following script:

from abc import ABC, abstractmethod
import dill


class A(ABC):

    @abstractmethod
    def test(self, a):
        """Abstract method"""


class B(A):

    def test(self, a):
        return a + 1


if __name__ == '__main__':
    test_obj = B()
    with open('test_save.pkl', 'wb') as f:
        dill.dump(test_obj, f)

When I run this script in a python 3.6 environment, it runs successfully. When I run it in a python 3.7.1 environment, it errors with:

TypeError: can't pickle _abc_data objects

One work around I have found is to move the class definition of B into a separate file and import it - then saving works. HOWEVER, assuming I do NOT want to do that:

1) is there a way to serialize with dill a subclass of an abstractclass defined in the same file

2) what changed between python 3.6 and python 3.7 that caused this?

hchw
  • 1,388
  • 8
  • 14
  • 1
    Possibly related: https://stackoverflow.com/questions/59058588/pyspark-got-typeerror-can-t-pickle-abc-data-objects – chepner Mar 07 '20 at 23:30
  • 1
    Looks like `ABCMeta` was implemented in pure Python in 3.6, but reimplemented in C for Python 3.7. – chepner Mar 07 '20 at 23:34

0 Answers0