问题描述
我正在遍历Python的 ast
模块,无法弄清切片的定义:
I'm wading through Python's ast
module and can't figure out the slices definition:
slice = Ellipsis | Slice(expr? lower, expr? upper, expr? step)
| ExtSlice(slice* dims)
| Index(expr value)
到目前为止,我知道 Ellipsis
是 [...]
, Slice
是通常的 [开始: end:step]
表示法, Index
是 [index]
,但哪种表示法是 ExtSlice
?
So far, I know that Ellipsis
is [...]
, Slice
is the usual [start:end:step]
notation, Index
is [index]
, but which notation is ExtSlice
?
推荐答案
扩展切片是具有多个部分的切片,使用某些特定于切片的功能。
An extended slice is a slice with multiple parts which uses some slice-specific feature.
特定于切片的功能类似于 ...
(文字省略号)或:
(一个测试分隔符)。
A slice specific feature is something like ...
(a literal ellipsis) or a :
(a test separator).
因此,举一个 ExtSlice
与诸如 o [...:None]
或 o [1,2:3]
。
So, an example where ExtSlice
is involved for an expression like o[...:None]
or o[1,2:3]
.
以下是一些演示此示例:
Here are some examples demonstrating this:
>>> compile('o[x]', '<string>', 'exec', PyCF_ONLY_AST).body[0].value.slice
<_ast.Index object at 0xb72a9e6c>
>>> compile('o[x,y]', '<string>', 'exec', PyCF_ONLY_AST).body[0].value.slice
<_ast.Index object at 0xb72a9dac>
>>> compile('o[x:y]', '<string>', 'exec', PyCF_ONLY_AST).body[0].value.slice
<_ast.Slice object at 0xb72a9dcc>
>>> compile('o[x:y,z]', '<string>', 'exec', PyCF_ONLY_AST).body[0].value.slice
<_ast.ExtSlice object at 0xb72a9f0c>
>>>
这篇关于Python AST中的ExtSlice节点表示什么语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!