返回顶部

收藏

python自定义list

更多
class MyList:
    def __init__(self, start):
        #self.wrapped = start[:]                 # Copy start: no side effects
        self.wrapped = []                        # Make sure it's a list here.
        for x in start: self.wrapped.append(x)
    def __add__(self, other):
        return MyList(self.wrapped + other)
    def __mul__(self, time):
        return MyList(self.wrapped * time)
    def __getitem__(self, offset):
        return self.wrapped[offset]
    def __len__(self):
        return len(self.wrapped)
    def __getslice__(self, low, high):
        return MyList(self.wrapped[low:high])
    def append(self, node):
        self.wrapped.append(node)
    def __getattr__(self, name):                 # Other members: sort/reverse/etc
        return getattr(self.wrapped, name)
    def __repr__(self):
        return self.wrapped

if __name__ == '__main__':
    x = MyList('spam')
    print x
    print x[0]
    print x[1:]
    print x + ['eggs']
    print x * 3
    x.append('a')
    x.sort( )
    for c in x: print c,

标签:Python,Class

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-03-19 11:48:35python多态,重写父类方法 by 风云轩
  2. 2015-03-30 09:02:05Inherited method by 蟋蟀MM
  3. 2015-04-21 09:36:21python的 __getitem__方法示例 by 西国の海妖
  4. 2015-04-30 09:02:17python重写父类方法 by 大猪
  5. 2015-05-07 12:00:04Function defined outside the class by 大猪
  6. 2015-03-30 19:29:21Use Inheritance to add more features to a class by 山药
  7. 2015-04-21 09:55:10Python用户定义类实现 by 阿明娃儿
  8. 2015-04-30 10:31:58python中使用self参数 by 山药
  9. 2015-05-08 09:01:24python类继承dict by jack.chen
  10. 2015-03-12 10:02:59Virtual function in Python by xwz
  11. 2015-04-10 17:19:06python类实例设置 by 大猪
相关聚客文章
  1. fanhuafeng 发表 2014-03-12 08:41:55 python学习之类(class)
  2. 博主 发表 2013-01-28 15:43:29 How to compile Python code to Java .class
  3. Kevin Shao 发表 2014-05-12 11:58:48 Python高级特性 Part III: Classes和Metaclasses
  4. 博主 发表 2013-11-23 08:00:00 Python class 入门
  5. 博主 发表 2013-01-28 15:43:29 How to compile Python code to Java .class
  6. ashione 发表 2015-04-17 16:06:56 python argumentParser objects
  7. Harrison Feng 发表 2014-06-05 22:00:00 Lists in Python
  8. daoser 发表 2013-04-19 03:29:31 python在终端运行的编码问题
  9. cpiz 发表 2012-03-18 11:27:03 用Python实现QQ找茬游戏外挂工具
  10. 博主 发表 2015-03-31 09:17:48 Python 实用技巧(一)
  11. 博主 发表 2015-03-12 16:56:33 python设置windows环境变量
  12. 博主 发表 2015-03-13 08:33:04 Python练习(二)

发表评论