开发者

Unpickling a Python class

开发者 https://www.devze.com 2023-01-08 03:21 出处:网络
I have a problem trying to unpickle subclasses of this class. When I unpickle开发者_C百科 it, the stuff isn\'t there. What gives?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消