Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Makes un-pickle-able objects pick-able.

Project description

PyPI-License PyPI-Version

Makes un-pickle-able objects pick-able.


You can install it via pip

pip install pickle-mixin

Running the tests

After installation, you can test it

python -c "import pickle_mixin; pickle_mixin.test()"

as long as you have pytest.


Pickle by initialisation

Suppose that you have a class whose objects are un-pickle-able or that would demand a large amount of disk space or memory to be pickle-able. PickleByInit class lets you pickle object attributes via object initialization. Consider the following classes:

class Foo(PickleByInit):
    def __init__(self, obj):
        super(Foo, self).__init__()
        self.obj = obj

class Bar(object):
    def __init__(self, filename):
        self.filename = filename

    def __getstate__(self):
        raise PicklingError

    def init_dict(self):
        return dict(filename=self.filename)

Trying to pickle as follows

f = Foo(Bar('file.txt'))

would raise a PicklingError. The following on the other hand would work:

f = Foo(Bar('file.txt'))

The un-pickling process of f.obj attribute happens via object initialisation, passing the returned dictionary from init_dict as keyword arguments to Bar.__init__.

Mixing classes with and without slots

Pickling does not save attributes defined via __slots__ in the following case:

class Foo(object):
    __slots__ = ['a']

    def __init__(self):
        self.a = 4

class Bar(Foo):
    def __init__(self):

SlotPickleMixin fixes it:

class FooMixin(object):
    __slots__ = ['a']

    def __init__(self):
        self.a = 4

class BarMixin(FooMixin, SlotPickleMixin):
    def __init__(self):

f = BarMixin()
o = pickle.dumps(f)
f = pickle.loads(o)
assert hasattr(f, 'a')



This project is licensed under the MIT License - see the LICENSE file for details

Project details

Release history Release notifications

This version
History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pickle-mixin-1.0.2.tar.gz (5.1 kB) Copy SHA256 hash SHA256 Source None Apr 9, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page