I have different python classes and I want to convert instances of these classes to ascii strings.Suppose that I have the following objects:
class A:
name = "A"
field = 2
x = "a"
class B:
name = "b"
item = "c"
a = A()
b = B()
I want a function like serialize() which converts the objects as follows:
serialize(a) # results "A2a"
serialize(b) # results "bc"
I don't want to write a serialization function for every classe, I want to have a function capable of serializing all classes. One way is to use the dir() function to get a list of object attributes and then create a string out of them, but the dir() function does not return the attributes in the same order that they are defined. For example calling dir(a) would return ['__doc__', '__module__', 'filed', 'name', 'x'], and I cannot find out which attribute is defined first in the class.
Thanks.
serializemethod in each of these classes be a valid option?