" "custom array classes to implement functions such as arithmetic, inner product operation, size comparison, array element access modification and member testing among the numbers in the array" "classMyArray:" "guaranteed input values are numeric elements (integer, float, plural)" " def ___isnumber(self, n):if notisinstance (n, (Int,float,complex)):returnFalsereturnTrue#constructor to perform the necessary initialization def __init__(self,*args):if notargs:self.__value= [] Else: forArginchargs:if notSelf.___isnumber(ARG):Print('All elements must is numbers') returnSelf .__value=list (args) @propertydefGetValue (self):returnSelf.__value #destructors, releasing the list of internal wrappers def __del__(self):delSelf.__value #overloaded operator + def __add__(self, Other):" "each element in the array is added to the number other, or two arrays are added to get a new array" " ifSelf.___isnumber(Other):#the array is added to the numberb =MyArray () b.__value= [item + Other forIteminchSelf.__value] returnbelifisinstance (other,myarray):#two array of corresponding elements added if(Len (Other).__value) = = Len (self.__value)): C=MyArray () c.__value= [I+j forI,jinchZip (self.__value, other.__value)] returnCElse: Print('Lenght No equal') Else: Print('Not supported') #Overloaded Operators- def __sub__(self, Other):" "The array element is subtracted from the number other and a new array is obtained" " Pass #Overloaded Operators * def __mul__(self, Other):" "The array element is multiplied with the number other, or two arrays, resulting in a new array" " Pass #overloaded array len, supports object directly using the Len () method def __len__(self):returnLen (self.__value) #support for viewing the value of an object using the print () function def __str__(self):returnSTR (self.__value)if __name__=="__main__": Print('Please use me as a module.') x= MyArray (1,12,15,14,1) Print('%s\n Array lenghts:%d'%(X,len (x))) x= x+2Print(X.getvalue)
Custom array classes in python3.4 (that is, overriding array classes)