Unpickling a Python class
I have a problem trying to unpickle subclasses of this class. When I unpickle开发者_C百科 it, the stuff isn't there. What gives? class Account:
def __init__(self, server, port, smtp_server, smtp_port):
self.server = server
self.port = port
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.save()
def save(self):
#save account for later loading
self.name = tkFileDialog.asksaveasfilename(title = "Save as..")
pickle.dump(self, open(self.name, "wr"))
Does your class inherit object
?
Either way, you can specify what you want to pickle by overwriting __getstate__
. Otherwise it should normally copy __dict__
if you're inheriting object
.
So, Here's how I just figured it out- i moved the ugly pickle stuff (see comment) to the unpickling class, imported the classes I was pickling, and it seems like it works.
精彩评论