The following optional methods can be defined to further emulate sequence objects. Immutable sequences methods should at most only define __getslice__(); mutable sequences might define all three methods.
定义以下方法可以进一步模拟有序类型对象; 不可变的有序类型应该只定义方法__getslice__(); 可变类型应该以下所有的方法.
self, i, j) |
从2.0版本开始, 这个函数不再推荐使用. 现在可以使用方法__getitem__()来实现它的功能.
Called to implement evaluation of self[i:j]
.
The returned object should be of the same type as self. Note
that missing i or j in the slice expression are replaced
by zero or sys.maxint
, respectively. If negative indexes are
used in the slice, the length of the sequence is added to that index.
If the instance does not implement the __len__() method, an
AttributeError is raised.
No guarantee is made that indexes adjusted this way are not still
negative. Indexes which are greater than the length of the sequence
are not modified.
If no __getslice__() is found, a slice
object is created instead, and passed to __getitem__() instead.
在对self[i:j]求值时调用本方法. 返回的对象应该与self的类型相同. 注意如果片断中缺少i或j, 它们就是分别替换成0或sys.maxint. 如果在片断中使用了负数, 运算时就是它加上有序类型对象的长度. 如果实例未实现__len__(), 则抛出AttributeError异常. 不能保证这样调整后的索引就是正数. 对于大于有序类型对象长度的索引是不做修改的. 如果没有找到__getslice__(), 就创建一个片断对象, 然后传递给__getitem__().
self, i, j, sequence) |
self[i:j]
.
Same notes for i and j as for __getslice__().
在对self[i:j]赋值时调用, 在__getslice__()中对于i和j的注意也适用于本方法.
This method is deprecated. If no __setslice__() is found,
or for extended slicing of the form
self[i:j:k]
, a
slice object is created, and passed to __setitem__(),
instead of __setslice__() being called.
本方法已经过时了. 如果没有找到__setslice__(), 就创建一个片断对象, 然后传递给__setitem__().
self, i, j) |
self[i:j]
.
Same notes for i and j as for __getslice__().
This method is deprecated. If no __delslice__() is found,
or for extended slicing of the form
self[i:j:k]
, a
slice object is created, and passed to __delitem__(),
instead of __delslice__() being called.
在删除self[i:j]时调用本方法.在__getslice__()中对i和j的注意也适用于本方法.本方法已经过时了. 如果没有找到__delslice__(), 就创建一个片断对象, 然后传递给__delitem__().
Notice that these methods are only invoked when a single slice with a single colon is used, and the slice method is available. For slice operations involving extended slice notation, or in absence of the slice methods, __getitem__(), __setitem__() or __delitem__() is called with a slice object as argument.
注意这些方法只在使用单冒号的片断语法时调用.对于扩展的片断记法或者缺少这些片断方法的情况下, 就调用__getitem__(), __setitem__() 或 __delitem__() , 以一个片断对象为参数,
The following example demonstrate how to make your program or module compatible with earlier versions of Python (assuming that methods __getitem__(), __setitem__() and __delitem__() support slice objects as arguments):
以下程序对如何你的程序兼容以前版本的Python做了一个示范(假定__getitem__(), __setitem__() 和 __delitem__()支持以片断对参数.)
class MyClass: ... def __getitem__(self, index): ... def __setitem__(self, index, value): ... def __delitem__(self, index): ... if sys.version_info < (2, 0): # They won't be defined if version is at least 2.0 final def __getslice__(self, i, j): return self[max(0, i):max(0, j):] def __setslice__(self, i, j, seq): self[max(0, i):max(0, j):] = seq def __delslice__(self, i, j): del self[max(0, i):max(0, j):] ...
Note the calls to max(); these are necessary because of
the handling of negative indices before the
__*slice__() methods are called. When negative indexes are
used, the __*item__() methods receive them as provided, but
the __*slice__() methods get a ``cooked'' form of the index
values. For each negative index value, the length of the sequence is
added to the index before calling the method (which may still result
in a negative index); this is the customary handling of negative
indexes by the built-in sequence types, and the __*item__()
methods are expected to do this as well. However, since they should
already be doing that, negative indexes cannot be passed in; they must
be constrained to the bounds of the sequence before being passed to
the __*item__() methods.
Calling max(0, i)
conveniently returns the proper value.
注意调用max()的代码; 它是必要的, 因为在调用__*slice__()之前要对负数片断进行控制.在使用负数索引后,方法__*item__()会获得和在提供该参数时的形式相同的数据. 但__*slice__()方法会得到一个包装过的索引值参数.对于每个负数索引值, 在调用这个方法前会加上该有序类型的长度(结果仍然可能是负数). 这个特征可以用来对内建有序类型的负数索引进行定制处理, 并且__*item__()系列方法是可以用于完成这个工作.但是由于负数索引应该已经被处理过了, 因此负数索引是不可能传进来的; 它们应该在传递进__*item__()方法之前就被限制在有该有序类型的长度范围之内, 通常用调用max(0,i)返回适当的值.