Skip to content
Advertisement

Custom list class slicing functionality

I want to implement my own type of list called Stack. I also want to add slicing functionality with the __getitem__ method like so:

JavaScript

Now if I create a Stack instance and append some elements to it I get the Stack type:

JavaScript

but if I slice my Stack, it becomes a list again:

JavaScript

how can I make st_sliced to stay Stack type after slicing?

Advertisement

Answer

return self.items[slc] return a list – this is why the type is not a Stack anymore.

The code below keep the type as Stack.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement