码迷,mamicode.com
首页 > 其他好文 > 详细

对象序列化

时间:2019-03-19 01:13:05      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:stat   对象序列化   str   title   ini   sel   one   int   对象   

import pickle
class DVD:
    def __init__(self,tilte,year=None,duration=None,director_id=None):
        self.title=tilte
        self.year=year
        self.duration=duration
        self.director_id=director_id
        self.filename=self.title.replace(,,_)+.pickle
    def save(self,filename=None):
        with open(self.filename,wb)as fh:
            pickle.dump(self,fh)
    @staticmethod
    def load(filename):
        with open(filename,rb)as fh:
            return pickle.load(fh)
    def __str__(self):
        return repr(self)
    def __repr__(self):
        return "DVD(‘{0}‘,{1},{2},‘{3}‘)".format(self.title,self.year,self.duration,self.director_id)
dvd1=DVD(Birds,2016,1,Justin Lin)
dvd1.save()
dvd2=DVD.load(Birds.pickle)
print(dvd2)

 

对象序列化

标签:stat   对象序列化   str   title   ini   sel   one   int   对象   

原文地址:https://www.cnblogs.com/larken/p/10556102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!