0

早上好,

我想创建一个从列表继承的类,例如我可以做的:

import pickle

object_list = []

object_list.append(an_object)
object_list.append(an_other_object)

object_list.dump('/home/user/my_file')

或者

import pickle

[an_objet, an_other_object].dump('/home/user/my_file')

我只是不知道如何创建我的课程,

谢谢阅读

4

1 回答 1

0

我知道了

import pickle

class my_list(list):
  def__init___(self, *args)
    list.__init__(self, *args)

  def dumps(self, path):
    with open(path, 'wb') as f:
      pick = pickle.Pickler(f)
      pick.dump(self)

  def load(self, path):
    with open(path, 'rb') as f:
      pick = pickle.Unpickler(f)
      for i in pick.load():
        self.append(i)
于 2013-09-23T22:58:47.127 回答