Object serialization: Difference between revisions

no edit summary
m (Switch to header template)
No edit summary
Line 274:
)
puts "END LOADED DIVERSE COLLECTION"
 
=={{header|Python}}==
<pre>
# Object Serialization in Python
# serialization in python is accomplished via the Picke module.
# Alternatively, one can use the cPickle module if speed is the key,
# everything else in this example remains the same.
 
import pickle
 
class Entity:
def __init__(self):
self.name = "Entity"
def printName(self):
print self.name
 
class Person(Entity): #OldMan inherits from Entity
def __init__(self): #override constructor
self.name = "Cletus"
 
instance1 = Person()
instance1.printName()
 
instance2 = Entity()
instance2.printName()
 
target = file("objects.dat", "w") # open file
 
# Serialize
pickle.dump((instance1, instance2), target) # serialize `instance1` and `instance2`to `target`
target.close() # flush file stream
print "Serialized..."
 
# Unserialize
target = file("objects.dat") # load again
i1, i2 = pickle.load(target)
print "Unserialized..."
 
i1.printName()
i2.printName()
</pre>