When it comes to sequences, we first think of a set of ordered elements. At the same time, each element has a unique subscript as the index.
In Python, there are many sequences of inner bounds. Includes tuple tuples, list lists, string str, and so on. One common feature of the sequence types (LIST,TUPLE,STR) mentioned above is that when a sequence object is created, it is necessary to open up a dedicated memory space to hold all the elements in the sequence. In other words, these sequence objects are essentially a collection.
For example, the following code creates a Sequence object S. When the sequence of objects is created, it is necessary to open up memory space to save 3 elements (integer-to-one) in the sequence.
s=[1,2,3]
However, according to the official Python document, a sequence object does not have to hold all the elements. In general, a sequence object needs to implement at least two of the following methods.
- __len__ method. The method returns the length of the sequence, which is the number of elements in the sequence.
- __getitem__ method. The method has an integer parameter (which may be remembered as index). It needs to return the value of the element that is labeled index in the sequence.
For example, the following code defines a sequence type.
classMyRange:def __init__(self, start, end): Self.start=Start Self.end=Enddef __len__(self):returnSelf.end-Self.startdef __getitem__(self, index):ifIndex < 0orIndex >=len (self):RaiseIndexerrorreturnIndex + Self.start
It defines a sequence of all integers from start to end-1.
- The __len__ method in the code returns the length of the sequence.
- The __getitem__ method in the code returns the index element in the sequence. Where the 第10-11 line determines whether the index is out of bounds. It is worth mentioning that the Len method called in line 10th is the built-in method of Python, which invokes the __len__ method of the Sequence object. as you can see, the __getitem__ method actually implements the formula of the Sequence object's pass term.
The following test code
MyRange = myrange (0, ten) print myrange[9] print
The output is as follows
9 Traceback (most recent): "test.py" in <module > print myrange[10] "test.py"in_ _getitem__ raise indexerror
Of course, in Python, the subscript of a sequence can be negative. Therefore, we make the following modifications to the __getitem__ method.
class MyRange: def __getitem__ (self, Index): if Else index + self.end ifor index >= len (self): Raise indexerror return
Test code
MyRange = myrange (0, ten) print myrange[-1] print
Output results
9 8
With the introduction above, we can easily understand that the range method in Python is different from the Xrange method.
- The Range method returns a list object that needs to open a special space to hold all the elements in the sequence.
- The Xrange method returns a Xrange object, which is a sequence object, but does not save the elements in the sequence. The implementation method is similar to the myrange type described in this article.
Therefore, if the sequence is read only, the Xrange method is more efficient, but if you need to change the elements of the sequence, or if you need to delete elements from the sequence, you can only generate a list object from the Range method.
Transferred from: http://blog.csdn.net/hedan2013/article/details/55000018
Python in xrange and Range (GO)