Hi Siddharth and Bruce The original poster asked for something like:
>>> lst = list(range(100)) >>> lst.slice(10, 20) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] For what it's worth, I think such a thing could be useful. And that experience of it's use would be needed, before it could be added to Python. And here's how to obtain such experience. Bruce has pointed out that we can already get this output: >>> lst[slice(10, 20)] [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] For what it's worth, here's another way: >>> lst.__getitem__(slice(10, 20)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] We can refactor the original poster's request into two parts. 1. A function, obtained from lst, with a particular property. 2. That this function be obtained in a particular way. We can achieve the first right now: >>> def slice_seq_factory(seq): ... def slice_seq(start, stop): ... return seq[start:stop] ... return slicer >>> slice_my_lst = slice_seq__factory(lst) >>> slice_my_lst(10, 20) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] And we don't need to store the intermediate result: >>> slice_seq__factory(range(100))(10, 20) range(10, 20) >>> slice_seq__factory(list(range(100)))(10, 20) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] An alternative approach is to subclass list (which we're allowed to do): >>> class mylist(list): ... def slice(self, start, stop): ... return slice_seq_factory(self)(start, stop) >>> lst = mylist(range(100)) >>> lst.slice(10, 20) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] I hope this helps. -- Jonathan
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/5UY76CVISV43FT23OBGI2HUFDUAXT7SS/ Code of Conduct: http://python.org/psf/codeofconduct/