Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Can not dump class object created on runtime

Reply
Thread Tools

Re: Can not dump class object created on runtime

 
 
Chris Rebert
Guest
Posts: n/a
 
      06-10-2009
On Wed, Jun 10, 2009 at 7:25 AM, Metal Zong<(E-Mail Removed)> wrote:
> 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


pickle stores classes by storing their fully-qualified name (eg.
"foo.bar.Baz") and NOT by storing the internal Python structures that
represent the class (as there are apparently various problem
associated with this). So when it unpickles, it just imports the
fully-qualified name normally and returns the result of that.
Since dynamically-created classes have no fully-qualified name, they
can't be stored this way; hence, you get an exception when trying to
pickle.dump() them.

Also, you shouldn't use `classobj` as I believe that's only for
old-style classes, which are being phased out. Its replacement is the
built-in `type` metaclass, which creates new-style classes.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dump complete java VM state as core dump (not via OS) possible? halfdog Java 12 02-21-2013 06:14 AM
ScriptManager and Form in pages created at runtime in class librar Pedro Ferreira ASP .Net 1 04-11-2008 02:01 PM
How to find at Runtime, if Created class object is instance of given class declaration Ami C++ 3 02-27-2006 04:59 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments