Python覆盖对象列表

时间:2013-09-23 18:01:07

标签: python list class override python-3.3

早上好,

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

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')

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

感谢您阅读

1 个答案:

答案 0 :(得分: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)