When to use ExtSlice node in Python's AST?

£可爱£侵袭症+ 提交于 2020-04-11 06:40:07

问题


Green Tree Snakes gives an example of using ExtSlice:

>>> parseprint("l[1:2, 3]")
Module(body=[
    Expr(value=Subscript(value=Name(id='l', ctx=Load()), slice=ExtSlice(dims=[
        Slice(lower=Num(n=1), upper=Num(n=2), step=None),
        Index(value=Num(n=3)),
      ]), ctx=Load())),
  ])

However this syntax won't work in interactive python shell:

>>> foo = range(10)
>>> foo[1:2,3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple

Anyone got an idea how to use this feature?

Related discussion:

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

回答1:


The syntax works fine in the shell, it is just that list objects don't support extended slicing. What you tried raised a TypeError, not a SyntaxError.

Many Numpy array types do; that project was instrumental in driving the extended slicing syntax. Numpy arrays use extended slicing to address the different dimensions of multi-dimensional arrays. See the Numpy Indexing chapter for details on how they use the syntax.

Extended slicing is explicitly documented in the Subscription section, the AST nodes encode the extended_slicing term:

extended_slicing ::=  primary "[" slice_list "]"
slice_list       ::=  slice_item ("," slice_item)* [","]
slice_item       ::=  expression | proper_slice | ellipsis
proper_slice     ::=  short_slice | long_slice

There are no types in the Python standard library itself that make use of extended slicing, however.

You can easily build your own class to accept an extended slice; just expect to handle a tuple in your object.__getitem__() method implementation:

>>> class Foo(object):
...     def __getitem__(self, item):
...         return item
...
>>> foo = Foo()
>>> foo[1, 2:3]
(1, slice(2, 3, None))

Each element of the slice_list becomes an object in a tuple, with :-separated slice indices passed in as slice() instances.



来源:https://stackoverflow.com/questions/35815397/when-to-use-extslice-node-in-pythons-ast

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