返回顶部

收藏

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. fanhuafeng 发表 2014-03-12 08:41:55 python学习之类(class)
  2. Kevin Shao 发表 2014-05-12 11:58:48 Python高级特性 Part III: Classes和Metaclasses
  3. 博主 发表 2013-01-28 15:43:29 How to compile Python code to Java .class
  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. wklken 发表 2013-07-19 16:00:00 [翻译整理]stackoverflow python 百问
  7. wklken 发表 2014-08-05 16:00:00 Python 源码阅读 - int
  8. mengskysama 发表 2015-01-15 07:44:39 Java RSA/ECB/NoPadding in C# Python
  9. Iven Hsu (ivenvd@gma 发表 2010-09-23 04:45:00 使用 dbus-python 建立单实例进程
  10. 博主 发表 2013-07-06 16:41:36 PyBitMessage与Gevent
  11. 博主 发表 2011-06-15 00:00:00 WeiBoPay
  12. 博主 发表 2013-10-26 19:15:00 pySerial -- Python的串口通讯模块

发表评论