'How to preserve args for unpickling object without storing them

I have a class with the following

def __init__(self, foo, **kwargs):
    self._foo = foo
    self._bar = []
    # transform foo to become bar

I want to pickle and then unpickle an object of this class and have implemented the __reduce__ method to allow me to do this:

    def __reduce__(self):
        return self.__class__, (self._foo,)

Ideally I wouldn't want to save foo as an instance variable and would only be using bar, the transformed version of foo. Is there a way that would allow me to unpickle the object using the original args without having to store foo? Perhaps moving the creation of _bar to another method?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source