Custom array classes in python3.4 (that is, overriding array classes)

Source: Internet
Author: User

" "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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.