return self.var[:]
What will that return?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Python permits you to “slice” various container types; this is a shorthand notation for taking some subcollection of an ordered collection. For instance, if you have a list
and you want the second, third, and fourth elements, you can do:
If you omit one of the numbers in the slice, it defaults to the start of the list. So for instance
Naturally, if you omit both numbers in the slice you will get the entire list back! However, you will get a copy of the list instead of the original; in fact, this is the standard notation for copying a list. Note the difference:
This occurs because
b = atellsbto point to the same object asa, so appending tobis the same as appending toa. Copying the listaavoids this. Notice that this only runs one level of indirection deep — ifacontained a list, say, and you appended to that list inb, you would still changea.By the way, there is an optional third argument to the slice, which is a step parameter — it lets you move through the list in jumps of greater than 1. So you could write range(100)[0::2] for all the even numbers up to 100.