Can not dump class object created on runtime

Metal Zong metalzong at 163.com
Wed Jun 10 10:25:24 EDT 2009


Hello,

Can not dump class object created on runtime.

Is there anybody can help me? Thank.

Following is testing code:

import pickle
from new import classobj

class A:
    def __str__(self):
        return self.__class__.name
    
if __name__ == "__main__":
    c = classobj('B', (A, ), {}) # create class obj on runtime
    print c
    print pickle.dumps(c) # get dump string

Bellows are outputs:

__main__.B
Traceback (most recent call last):
  File "C:\USERS\train\_work\test\test.py", line 11, in <module>
    print pickle.dumps(c)
  File "c:\USERS\train\Python25\lib\pickle.py", line 1366, in dumps
    Pickler(file, protocol).dump(obj)
  File "c:\USERS\train\Python25\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "c:\USERS\train\Python25\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\USERS\train\Python25\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <class __main__.B at 0x00AF4CF0>: it's
not found as __main__.B

- Tommy
HZ 23026
MP 13958175281

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090610/603ccd1f/attachment.html>


More information about the Python-list mailing list