I want to serialize the class itself rather than a object instance.
For example if I do this
class Foo:
pass
json.dumps(Foo)
It would throw an error saying Foo is not JSON serializable. Is it even possible to do this with python?
A bit late, but you can't actually directly serialize a class. Serializing is the act of turning an object into a string for storage and transmission. A class declaration, in your case Foo, is already a text file; Foo.py (or wherever you put it).
Serialization librairies usually keep the object's class in reference, so that the same program quand save and load an object and still know that it's a Foo instance. But, if I program my own little app that does not contain a Foo class, then unserializing your data will give me something that is pretty much useless.
Depending on what you want to do, there are alternatives.
For the last option, you, in a way, want to build a class of classes:
Class model:
def __init__(self, class_name, fields):
self.class_name = class_name
self.fields = fields
Class model_instance:
def __init__(self, model, values):
self.fields = {}
# add values to the dict with fields as keys, based on the model
...
When you instantiate a "model", you are actually creating the model of an object. There would then be multiple tricks to correctly implement an alternative instantiation system with this, at this point, some research will be required.
I understand this post is a bit old but I thought answering what I know while searching for what I don't is the entire point of Stack Overflow!
shelvesorpickle