473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem pickling objects created with the type function

Howdy,

I've run into a problem pickling objects created with a type
statement. Here's the code:

*********************************************
import pickle

class foo(object):
def __init__(self):
self.wombat = []

def setWombat(self):
self.wombat = [1]

# pickle a foo instance
x = foo()
print pickle.dumps(x) # works

# pickle a foobar instance
childType = type('foobar',(foo,),{'slor':7})
y = childType()
print pickle.dumps(y) # raises an exception
*********************************************

I'm pretty sure the failure is because 'foobar' is never in the global
namespace. If I change the code to foobar = type('foobar',... then
the code works, but I dont' want to do this because I create the class
name in a factory and it is mangled to prevent different invocations
of the factory from having the same class name.

Does anyone know how to get around this, or how to get 'foobar' =
childType into the global namespace?

thanks,
Danny
Jul 18 '05 #1
2 1159
Yep.

Pickle stores instances by pickling information about the instance, plus
a string it uses to find the class.

If you do something too clever, like you did above, it doesn't work.

Jeff

Jul 18 '05 #2
On 21 Apr 2004 14:57:17 -0700, da***********@yahoo.com (danny) wrote:
I'm pretty sure the failure is because 'foobar' is never in the global
namespace. If I change the code to foobar = type('foobar',... then
the code works, but I dont' want to do this because I create the class
name in a factory and it is mangled to prevent different invocations
of the factory from having the same class name.

Does anyone know how to get around this, or how to get 'foobar' =
childType into the global namespace?


You should be able to make foobars pickleable without introducing foobar into
the global namespace by using a __reduce__ method, see:

http://www.python.org/peps/pep-0307.html

expecially the section "Extended __reduce__ API".

---
Greg Chapman

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
7
by: Jeffrey Barish | last post by:
(Pdb) myclass MyClass( 0, 0, 'A string', 123.45) (Pdb) copy.copy(myclass) *** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',) I see 4 arguments (actually, 5 because...
10
by: krustymonkey | last post by:
I'm wondering if anyone can help with a workaround for a problem I currently have. I'm trying to set up a prefork tcp server. Specifically, I'm setting up a server that forks children and has them...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.