Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python类\uuuu getitem\uuuuu负索引处理_Python_Class_Indexing - Fatal编程技术网

python类\uuuu getitem\uuuuu负索引处理

python类\uuuu getitem\uuuuu负索引处理,python,class,indexing,Python,Class,Indexing,是否有一个共同的功能来执行此操作?现在我正在做以下工作(并覆盖\uuu len\uuu) idx

是否有一个共同的功能来执行此操作?现在我正在做以下工作(并覆盖
\uuu len\uuu

idx<0:
idx=len(自)+idx
如果idx<0或idx>=len(自):
提升索引器,“数组索引(%d)超出范围[0,%d]”%(idx,len(self))

这对我来说似乎很好。我认为没有更好的或内置的方法来做到这一点;重写毕竟是提供您自己的功能


编辑以删除相当愚蠢的建议。

对于谷歌带来的那些建议,我想我应该添加我刚想到的内容。似乎对我很有用

def __getitem__(self, index):
    # Do this until the index is greater than 0.
    while index < 0:
        # Index is a negative, so addition will subtract.
        index += len(self)
    if index >= len(self):
        raise IndexError
    # Replace with your custom interface.
    return self.__instance.GetValue(index)
def\uuuu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
#执行此操作,直到索引大于0。
当指数<0时:
#索引是负数,所以加法会减法。
索引+=len(自)
如果索引>=len(自):
提升索引器
#替换为自定义接口。
返回self.\u实例.GetValue(索引)
def __getitem__(self, index):
    # Do this until the index is greater than 0.
    while index < 0:
        # Index is a negative, so addition will subtract.
        index += len(self)
    if index >= len(self):
        raise IndexError
    # Replace with your custom interface.
    return self.__instance.GetValue(index)