Python - slice([1,2,3]) 是如何工作的以及 slice(None, [1, 3], None) 代表什么?
Python - how slice([1,2,3]) works and what does slice(None, [1, 3], None) represent?
Documentation for class slice(start, stop[, step])
:
Return a slice object representing the set of indices specified by range(start, stop, step).
代码中发生了什么以及为什么切片 class 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
更新
感谢 Selcuk 的回答。
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
}
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
值是否有意义。
当您只传递一个参数时,它被假定为 stop
值。这就是为什么您最终将 start
和 step
值设置为 None
:
class slice(stop)
class slice(start, stop[, step])
Documentation for class slice(start, stop[, step])
:
Return a slice object representing the set of indices specified by range(start, stop, step).
代码中发生了什么以及为什么切片 class 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
更新
感谢 Selcuk 的回答。
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
}
Slice objects have read-only data attributes
start
,stop
andstep
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
值是否有意义。
当您只传递一个参数时,它被假定为 stop
值。这就是为什么您最终将 start
和 step
值设置为 None
:
class slice(stop)
class slice(start, stop[, step])