A view for Python list slices As you probably know, Python slices like arr[1:3] etc. do NOT provide a view onto list and instead creates a copy of the list, what can be not optimal performance if array is big and you make for example arr[1:] (copies all exept first) 
the idea of a view to an array is like :

class ylist_view():

    def __init__(self,lst, begin, end):

        self.list = lst
        self.len = end - begin
        self.begin = begin
        self.end = end

    def __getitem__(self, i):
        if i&lt;0: i += self.len
        return self.list[self.begin + i]

    def __setitem__(self, i,value):
        if i&lt;0: i += self.len
        self.list[self.begin + i] = value    

     def __str__(self):
            return  '[' + ', '.join( str(self.list[i]) for i in range(self.begin, self.end)) + ']'



class inbox():

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return '&lt;' + str(self.value) + '&gt;'

    def __repr__(self):
        return '&lt;' + str(self.value) + '&gt;'

    a =   [ inbox(x) for x in  [1,2,3,4,5]]

    b = a [1:3]

    b[0] = 20 # creates a copy

    print('list a is unchanged:', a)
    print('b = a [1:3] is a copy of a :', b)        

    c = ylist_view(a,1,3)

    c[0] = 20

    print('ylist_view c is a view to a:', a)


Inbox class is a dummy class for illustration.
As you can see, b = a [1:3] is a copy, whereas c = ylist_view(a,1,3) references the original array.
Does it make sense to use this approach in a real Python project or there is some built-in in Python to do that?
If you are dealing with number lists, then array.array has a compact memory representation and implements the buffer protocol which allows memoryview’s to be created, which directly support views like you are creating.

For lists which can hold other things (tuples, dictionaries, lambdas, ...), Python has no built in support, and your view class can be appropriate.



PEP8 guidelines

Your class name should begin with a capital letter.  I’d suggest ListView as an option.  I don’t know what the ‘y’ is intended to mean.

There should be one space after every comma.  There shouldn’t be any spaces between a variable name and the [ character (b = a[1:3]).

Private members (self.begin, etc) should begin with an underscore (self._begin, etc).

Use a pylint, pyflakes, ... to ensure PEP8 compliance. 



You could implement __repr__ in terms of __str__:

    def __repr__(self):
        return str(self)




Extension: Your list view could support a view slice with a step size other than 1.



You don’t protect against indexing beyond the length of your view. i &gt;= self.len.