2

What exactly does the following code do?

     slice(x,y) 

x,y are integers and it is being called on an object i.e.

     def view(self):
        return slice(self.x,self.y)

I am unfamiliar with Python libraries and haven't been able to find it explained clearly anywhere.

1 Answer 1

4

It's all documented here and here. The function returns a slice object which can be used to slice, e.g., a list. For example:

>>> my_slice = slice(0, 1)
>>> my_list = [1, 2, 3]
>>> my_list[my_slice]
[1]

This allows you to programmatically slice a list, instead of writing my_list[0:1].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.