[Tutor] To make a new pyarray class

Manprit Singh manpritsinghece at gmail.com
Wed Sep 1 12:07:40 EDT 2021


Dear sir,

I need to make a new pyarray class that can accept any number of arguments,
and is capable of returning a list-like object. all methods of lists can be
applied to the object of this pyarray class.

Other than this if i need a  + operator to be overloaded for this class, +
operator should add a scalar value (v) to each element of the instance of
this class. Like if an instance of class pyarray is x = [2, 5, 9] then  x+1
must produce and return  [3, 6, 10].

class pyarray(list):
     def __init__(self, *args):
         list.__init__(self, args)
     def __add__(self, x):
         return pyarray(*(ele+x for ele in self))

x = pyarray(3, 5, 8)   // pyarray class called and object x created which
is a list like object
print(x)   // gives below written output , the output is list like
[3, 5, 8]
x.append(6)
print( x)
[3, 5, 8, 6]   // list append method successfully worked.


More information about the Tutor mailing list