1 # encoding=utf-8
  2 from __future__ import division
  3 from collections import Iterable
  4 
  5 
  6 class Array(Iterable):
  7     """
  8     元素相同的列表 不用指定length
  9     """
 10 
 11     def check_type(self, ins):
 12         if not isinstance(ins, self.__type):
 13             raise TypeError('the Array element must be the same type')
 14 
 15     def __init__(self, seq=(), _type=int):
 16         if not type(_type) == type.__class__:
 17             "传递的是类实例"
 18             _type = type(_type)
 19         self.__type = _type
 20         map(self.check_type, seq)
 21         self.__value = list(seq)
 22 
 23     def __del__(self):
 24         del self.__value
 25 
 26     def __add__(self, other):
 27         """
 28         重载+运算 支持+n 或等长数组相加
 29         :param other: int or the same length Array
 30         :return: __value
 31         """
 32         assert isinstance(other, (int, float, Array))
 33         if isinstance(other, (int, float)):
 34             self.__value = [i + other for i in self.__value]
 35             return self.__value
 36         if len(self.__value) != len(other):
 37             raise Exception("the 'other' params length mast eq current instance length")
 38         self.__value = [i + j for i, j in zip(self.__value, other)]
 39         return self.__value
 40 
 41     def __sub__(self, other):
 42         """
 43         重载-运算 支持-n 或等长数组相减
 44         :param other: int or the same length Array
 45         :return: __value
 46         """
 47         assert isinstance(other, (int, float, Array))
 48         if isinstance(other, (int, float)):
 49             self.__value = [i - other for i in self.__value]
 50             return self.__value
 51         if len(self.__value) != len(other):
 52             raise Exception("the 'other' params length mast eq current instance length")
 53         self.__value = [i - j for i, j in zip(self.__value, other)]
 54         return self.__value
 55 
 56     def __mul__(self, other):
 57         """
 58         重载*运算 支持*n 或等长数组相乘
 59         :param other: int or the same length Array
 60         :return: __value
 61         """
 62         assert isinstance(other, (int, float, Array))
 63         if isinstance(other, (int, float)):
 64             self.__value = [i * other for i in self.__value]
 65             return self.__value
 66         if len(self.__value) != len(other):
 67             raise Exception("the 'other' params length mast eq current instance length")
 68         self.__value = [i * j for i, j in zip(self.__value, other)]
 69         return self.__value
 70 
 71     def __truediv__(self, other):
 72         """
 73         重载/运算 支持/n 或等长数组相/
 74         :param other: int or the same length Array
 75         :return: __value
 76         """
 77         assert isinstance(other, (int, float, Array)) and other != 0
 78         if isinstance(other, (int, float)):
 79             self.__value = [i / other for i in self.__value]
 80             return self.__value
 81         if len(self.__value) != len(other):
 82             raise Exception("the 'other' params length mast eq current instance length")
 83         self.__value = [i / j for i, j in zip(self.__value, other)]
 84         return self.__value
 85 
 86     def __floordiv__(self, other):
 87         """
 88         重载//运算 支持//n 或等长数组相//
 89         :param other:
 90         :return:
 91         """
 92         assert isinstance(other, (int, float, Array)) and other != 0
 93         if isinstance(other, (int, float)):
 94             self.__value = [i // other for i in self.__value]
 95             return self.__value
 96         if len(self.__value) != len(other):
 97             raise Exception("the 'other' params length mast eq current instance length")
 98         self.__value = [i // j for i, j in zip(self.__value, other)]
 99         return self.__value
100 
101     def __mod__(self, other):
102         """
103         重载%运算 支持%n 或等长数组相%
104         :param other:
105         :return:
106         """
107         assert isinstance(other, (int, float, Array)) and other != 0
108         if isinstance(other, (int, float)):
109             self.__value = [i % other for i in self.__value]
110             return self.__value
111         if len(self.__value) != len(other):
112             raise Exception("the 'other' params length mast eq current instance length")
113         self.__value = [i % j for i, j in zip(self.__value, other)]
114         return self.__value
115 
116     def __iter__(self):
117         """
118         可迭代对象
119         :return:
120         """
121         for i in self.__value:
122             yield i
123 
124     def __hash__(self):
125         """
126         可hash对象
127         :return:
128         """
129         return hash(id(self))
130 
131     def __eq__(self, other):
132         """
133         ==
134         :param other:
135         :return:
136         """
137         assert isinstance(other, self.__class__)
138         return self.__value == other.__value
139 
140     def __gt__(self, other):
141         """
142         >
143         :param other:
144         :return:
145         """
146         assert isinstance(other, self.__class__)
147         return self.__value > other.__value
148 
149     def __ge__(self, other):
150         """
151         <
152         :param other:
153         :return:
154         """
155         assert isinstance(other, self.__class__)
156         return self.__value >= other.__value
157 
158     def __getitem__(self, index):
159         """
160         按索引取值
161         :param index:
162         :return:
163         """
164         assert isinstance(index, int)
165         if index >= len(self):
166             raise IndexError('index out of range')
167         return self.__value[index]
168 
169     def __setitem__(self, index, value):
170         """
171         按索引修改
172         :param index:
173         :param value:
174         :return:
175         """
176         assert isinstance(index, int) and isinstance(value, self.__type)
177         if index >= len(self):
178             raise IndexError('index out of range')
179         self.__value[index] = value
180 
181     def __delitem__(self, index):
182         if not len(self):
183             raise IndexError('pop from empty array')
184         del self.__value[index]
185 
186     def __contains__(self, item):
187         """
188         in
189         :param item:
190         :return:
191         """
192         return item in self.__value
193 
194     def __len__(self):
195         return len(self.__value)
196 
197     def __str__(self):
198         return str(self.__value) + '\t\tArray for %s' % str(self.__type)
199 
200     def max(self):
201         """
202         the max value in array
203         :return: max element
204         """
205         return max(self.__value)
206 
207     def min(self):
208         """
209         the min value in array
210         :return: min element
211         """
212         return min(self.__value)
213 
214     def sort(self, *args, **kwargs):
215         """
216         sort by key
217         :param args:
218         :param kwargs: key=
219         :return:
220         """
221         return self.__value.sort(*args, **kwargs)
222 
223     def count(self, value):
224         """
225         count a value in array
226         :param value:
227         :return:
228         """
229         return self.__value.count(value)
230 
231     def index(self, value, start=None, stop=None):
232         """
233         find a value in [start, stop]
234         :param value:
235         :param start:
236         :param stop:
237         :return:
238         """
239         return self.__value.index(value, start, stop)
240 
241     def insert(self, index, value):
242         """
243         insert a value by index in array
244         :param index:
245         :param value:
246         :return:
247         """
248         assert isinstance(index, int)
249         return self.__value.insert(index, value)
250 
251     def pop(self):
252         """
253         删除数组最后一个元素
254         :return: 最后一个元素
255         """
256         if not len(self):
257             raise IndexError('pop from empty array')
258         return self.__value.pop()
259 
260     def popleft(self):
261         """
262         删除数组第一个元素
263         :return: 第一个元素
264         """
265         if not len(self):
266             raise IndexError('pop from empty array')
267         val = self[0]
268         del self[0]
269         return val
270 
271     def reverse(self):
272         """
273         reverse
274         :return:
275         """
276         self.__value.reverse()
277 
278     def remove(self, value):
279         """
280         remove by value
281         :param value:
282         :return:
283         """
284         self.__value.remove(value)
285 
286     def extend(self, _iter):
287         """
288         扩展ite元素
289         :param _iter: an iterable sequence
290         :return:
291         """
292         self.__value.extend(_iter)

实例化时, 类型可通过类名或实例传递, 如下:
  Array([1, 2, 3, 4], int)
  Array([1, 2, 3, 4], 1)