Unpickle Python 类
我在尝试解开此类的子类时遇到问题。当我拆开它时,东西不在那里。什么给? 班级帐号:
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"))
I have a problem trying to unpickle subclasses of this class. When I unpickle 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"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的类继承
object
吗?无论哪种方式,您都可以通过覆盖
__getstate__
来指定要腌制的内容。否则,如果您继承object
,它通常应该复制__dict__
。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 inheritingobject
.所以,这就是我刚刚想到的方法——我将丑陋的泡菜东西(参见评论)移到了 unpickling 类中,导入了我正在 pickle 的类,看起来它是有效的。
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.