The marshal module

Under the covers, the pickle module calls the marshal module to do some of its work, but most programs should not use marshal at all. The one advantage of marshal is that, unlike pickle, it can handle code objects (the implementation itself):

>>> def adder(a,b): ... return a+b >>> adder(10,2) 12

>>> import marshal

>>> s = marshal.dumps(adder.func_code) >>> def newadder(): ... pass

>>> newadder.func_code = marshal.loads(s) >>> newadder(20,10) 30

Chapter 33 shows you how to access code objects and other attributes of Python objects such as functions.


0 0

Post a comment