What syntax is represented by an ExtSlice node in Python's AST?

佐手、 提交于 2019-12-23 17:22:33

问题


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) 

So far, I know that Ellipsis is [...], Slice is the usual [start:end:step] notation, Index is [index], but which notation is ExtSlice?


回答1:


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).

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>
>>> 


来源:https://stackoverflow.com/questions/8370132/what-syntax-is-represented-by-an-extslice-node-in-pythons-ast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!