gpt4 book ai didi

Python - slice([1,2,3]) 如何工作以及 slice(None, [1, 3], None) 代表什么?

转载 作者:行者123 更新时间:2023-12-03 19:40:37 25 4
gpt4 key购买 nike

Documentation for class slice(start, stop[, step]) :

Return a slice object representing the set of indices specified by range(start, stop, step).


代码中发生了什么以及为什么切片类 init 甚至允许列表作为其参数?
print(slice([1,3]))
---
slice(None, [1, 3], None)

print(slice(list((1,3))))
---
slice(None, [1, 3], None) # why stop is list?

hoge = [1,2,3,4]
_s = slice(list((1,3)))
print(hoge[_s])
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-59-1b2df30e9bdf> in <module>
1 hoge = [1,2,3,4]
2 _s = slice(list((1,3)))
----> 3 print(hoge[_s])

TypeError: slice indices must be integers or None or have an __index__ method

更新
感谢塞尔丘克的回答。
sliceobject.c#L303-L322
static PyObject *
slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
PyObject *start, *stop, *step;

start = stop = step = NULL;

if (!_PyArg_NoKeywords("slice", kw))
return NULL;

if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
return NULL;

/* This swapping of stop and start is to maintain similarity with
range(). */
if (stop == NULL) {
stop = start; // <-----
start = NULL;
}

return PySlice_New(start, stop, step); // PySlice_New in L110 in the same file
}

最佳答案

来自 the documentation :

Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality [...]


所以它们只是保持你传递给它们的任何东西的虚拟对象。您甚至可以传递字符串或其他对象:
my_slice = slice("foo", "bar", "baz")

[...] however they are used by Numerical Python and other third party extensions.


验证 start 是否是第三方扩展程序的工作, stop , 和 step值(value)观有任何意义。
另见 CPython implementation .
当你只传递一个参数时,它被假定为 stop值(value)。这就是为什么你最终会得到 startstep值设置为 None :

class slice(stop)

class slice(start, stop[, step])

关于Python - slice([1,2,3]) 如何工作以及 slice(None, [1, 3], None) 代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65418944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com