## 2014年5月4日日曜日

その他参考書籍

コード(BBEdit)

sample.py

```#!/usr/bin/env python3
#-*- coding: utf-8 -*-

class MyList:
def __init__(self, data=[]):
self.data = list(data)
if isinstance(other, MyList):
other = other.data
return MyList(self.data + other)
return MyList(other + self.data)
def __getitem__(self, i):
return self.data[i]
def append(self, other):
self.data.append(other)
def sort(self,key=None, reverse=False):
self.data.sort(key=key, reverse=reverse)
def __getattr__(self, name):
return getattr(self.data, name)
def __repr__(self):
return 'MyList: {0}'.format(self.data)

if __name__ == '__main__':
o1 = MyList([1,2,3,4,5])
o2 = MyList([6, 7])
l = [10, 100]
print(o1 + o2)
print(o2 + o1)
print(o1 + l)
print(l + o1)
print(o1[2])
print(o1[:])
o1 += [1000]
print(o1)
o1.append(50)
print(o1)
o1.sort()
print(o1)
print(o1.pop())
print(o1)
for x in o1:
print(x)
s = MyList('python')
print(s)

```

```\$ ./sample.py
MyList: [1, 2, 3, 4, 5, 6, 7]
MyList: [6, 7, 1, 2, 3, 4, 5]
MyList: [1, 2, 3, 4, 5, 10, 100]
MyList: [10, 100, 1, 2, 3, 4, 5]
3
[1, 2, 3, 4, 5]
MyList: [1, 2, 3, 4, 5, 1000]
MyList: [1, 2, 3, 4, 5, 1000, 50]
MyList: [1, 2, 3, 4, 5, 50, 1000]
1000
MyList: [1, 2, 3, 4, 5, 50]
1
2
3
4
5
50
MyList: ['p', 'y', 't', 'h', 'o', 'n']
\$
```