class MyArray:
'''All the elements in this array must be numbers'''
def __IsNumber(self,n):
if not isinstance(n,(int,float,compile)):
return False
return True
#构造函数,进行必要的初始化
def __init__(self,*args):
if not args:
self.__value=[]
else:
for arg in args:
if not self.__IsNumber(arg):
print('All elements must be numbers')
return
self.__value=list(args)
#析构函数,释放内部
def __del__(self):
del self.__value
#重载运算符 +
#数组中每个元素都与数字other相加,或两个数组相加,返回新数组
def __add__(self, other):
if self.__IsNumber(other):
#数组中所有元素都与数字n相加
b = MyArray()
b.__value = [item+other for item in self.__value]
return b
elif isinstance(other,MyArray):
#两个等长的数组对应元素相加
if len(other.__value)==len(self.__value):
c = MyArray()
c.__value = [i+j for i,j in zip(self.__value,other.__value)]
return c
else:
print('Length not equal')
else:
print('Not supported')
#重载运算符 —
def __sub__(self, other):
if not self.__IsNumber(other):
print('- operating with',type(other),'and number type is not supported.')
return
b = MyArray()
b.__value = [item-other for item in self.__value]
return b
#重载运算符 *
#数组中每个元素都与数字other相乘,返回新数组
def __mul__(self, other):
if not self.__IsNumber(other):
print('* operating with',type(other),'and number type is not supported.')
return
b = MyArray()
b.__value = [item*other for item in self.__value]
return b
#重载运算符 /
#数组中每个元素都与数字other相除,返回新数组
def __truediv__(self, other):
if not self.__IsNumber(other):
print(r'/ operating with',type(other),'and number type is not supported.')
return
b = MyArray()
b.__value = [item/other for item in self.__value]
return b
#重载运算符 //
#数组中每个元素都与数字other整除,返回新数组
def __floordiv__(self, other):
if not isinstance(other,int):
print(other,'is not an integer')
return
b = MyArray()
b.__value = [item//other for item in self.__value]
return b
#重载运算符 %
#数组中每个元素都与数字other求余数,返回新数组
def __mod__(self, other):
if not self.__IsNumber(other):
print(r'% operating with',type(other),'and number type is not supported.')
return 0
b = MyArray()
b.__value = [item%other for item in self.__value]
return b
#重载运算 **
#数组中每个元素都与数字n进行幂计算,返回新数组
def __pow__(self, power, modulo=None):
if not self.__IsNumber(power):
print('** operating with',type(power),'and number type is not supported.')
return
b = MyArray()
b.__value = [item**power for item in self.__value]
return b
#重载长度计算
def __len__(self):
return len(self.__value)
#直接使用该类对象作为表达式来查看对象的值
def __repr__(self):
return repr(self.__value)
#支持使用print()函数查看对象的值
def __str__(self):
return str(self.__value)
#追加元素
def append(self,other):
if not self.__IsNumber(other):
print('Only number can be appended.')
return
self.__value.append(other)
#获取指定下标的元素值,支持使用列表或元组指定多个下标
def __getitem__(self, index):
length = len(self.__value)
#如果指定单个整数作为下标,则直接返回元素值
if isinstance(index,int) and 0<=index<length:
return self.__value[index]
elif isinstance(index,(list,tuple)):
for i in index:
if not (isinstance(i,int) and 0<=i<length):
return 'index error'
result = []
for item in index:
result.append(self.__value[item])
return result
else:
return 'index error'
#修改元素值,支持使用列表或元组指定多个下标,同时修改多个元素值
def __setitem__(self, index, value):
length = len(self.__value)
#如果下标合法,则直接修改元素值
if isinstance(index,int) and 0<=index<length:
self.__value[index] = value
#支持使用列表或元组指定多个下标
elif isinstance(index,(list,tuple)):
for i in index:
if not(isinstance(i,int)) and 0<=index<length:
raise Exception('index error')
#如果下标和给的值都是列表或元组,并且个数一样,则分别为多个下表的元素修改值
if isinstance(value,(list,tuple)):
if len(index) == len(value):
for i,v in enumerate(index):
self.__value[v] = value[i]
else:
raise Exception('values and index must be of the same length')
#如果指定多个下标和一个普通值,则把多个元素修改为相同的值
elif isinstance(value,(int,float,complex)):
for i in index:
self.__value[i] = value
else:
raise Exception('value error')
else:
raise Exception('index error')
#支持成员测试运算符in,测试数组中是否包含某个元素
def __contains__(self, item):
if item in self.__value:
return True
return False
#模拟向量内积
def dot(self,v):
if not isinstance(v,MyArray):
print(v,'Must be an instance of MyArray.')
return
if len(v) != len(self.__value):
print('The size must be equal.')
return
return sum([i*j for i,j in zip(self.__value,v.__value)])
#重载运算符号==,测试两个数组是否相等
def __eq__(self, other):
if not isinstance(other,MyArray):
print(other+'must be an instance of MyArray.')
return False
if self.__value == v.__value:
return True
return False
#重载运算<,比较两个数组大小
def __lt__(self, other):
if not isinstance(other,MyArray):
print(other,'must be an instance of MyArray.')
return False
if self.__value < other.__value:
return True
return False
if __name__ == '__main__':
print('Please use me as a module')
array = MyArray(1,2,4,6,7)
a=array.__mul__(2)
print(a)
#######输出######
Please use me as a module
[2, 4, 8, 12, 14]