A base class to make your Python classes permanent in a flash.



  • Easy to use.
  • Great compatibility.
  • No database needed.
  • Ask for new features through the issues section.
  • Join our Telegram group for support or development.


You can install or upgrade the module with:

pip install pyrmanent --upgrade

Usage guide

Basic usage:

Making your custom clases permanent is as easy as adding Pyrmanent as the base class.

from pyrmanent import Pyrmanent

class MyClass(Pyrmanent):

Call the save method to save the current instance data:

my_class_instance = MyClass()
my_class_instance.my_data = "Hello world!"

To load a previously saved instance data, just create the instance and data will be
automatically loaded:

my_class_instance = MyClass()

"Hello world!"


Instance name

The name parameter allows saving different instances of the same class. Each one will
keep its own data.

first_instance = MyClass(name="first")
first_instance.my_data = "This is the first instance"

second_instance = MyClass(name="second")
second_instance.my_data = "And this the second one"

Saving path

The path for the saved files can be customized with the folder parameter. If not provided,
the files will be saved on the execution folder.

my_class_instance = MyClass(folder="data")

Auto saving

The autosave parameter allows using the autosave method to save data only when True.
This is specially useful for your custom class methods.

class MyClass(Pyrmanent):
    def set_title(self, title):
        self.title = title

# The instance data will be saved when the method is called
my_class_instance = MyClass(autosave=True)

# You should manually save the instance data
my_class_instance = MyClass(autosave=False)

Instance initialization

Override the __init__ method

The __init__ method can be overrided to provide default configurations, like for example
the saving path in below example.

class MyClass(Pyrmanent):
    def __init__(self, name, autosave=True):
        super().__init__(name=name, folder="data", autosave=autosave)

The init method

On the other hand, you can define the init method (without the underscores) to initialize
attribute values. This method is called before saving the instance data for the first time.

class MyClass(Pyrmanent):
    def init(self):
        self.first_value = 1
        self.second_value = 2

Use dill instead of pickle

Dill extends the compatibility with several data types that pickle module can’t serialize.
It provides the same interface, so it’s totally compatible without any code change needed.
To start using dill, just install it using pip and it will be used by default:

pip install dill --upgrade

To stop using dill, just uninstall it. You can find more information about dill in the
official repo.


Copyright © 2021 Sergio Abad. See license for details.


View Github