Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep attribute outside __dict__?

I am creating a series of object instances, the attributes of which i save and load using their __dict__ attribute. I would like to keep some attributes outside of __dict__, simply because i do not want them to be saved or loaded.

More specifically:

I created a parent object merely holding a list of children objects. Children objects' __dict__ is saved to file and loaded later on to instantiate them again. I want the children objects to have a reference to the parent, but i do not want that reference to be saved to, nor loaded from file, as it would not make sense.

Is there any syntax that would exclude the attribute from __dict__?

PS: many answers suggest using pickle, i am currently using json to keep data human readable.

like image 750
neydroydrec Avatar asked Jun 13 '11 10:06

neydroydrec


2 Answers

Add some prefix for attributes you don't want to save and exclude them while saving.

like image 184
Roman Bodnarchuk Avatar answered Oct 18 '22 05:10

Roman Bodnarchuk


This approach won't work and so is not going to solve your problem. You should instead extend your framework to allow you to specify which attributes were to be omitted from the save/load.

However, it seems odd that you aren't using one of the built in persistence techniques, e.g. pickle, that already offer such features.

like image 27
David Heffernan Avatar answered Oct 18 '22 04:10

David Heffernan