何时在Python的AST中使用ExtSlice节点? [英] When to use ExtSlice node in Python's AST?

查看:148
本文介绍了何时在Python的AST中使用ExtSlice节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绿树蛇给出了使用 ExtSlice 的示例

 >> parseprint( l [1:2,3])
Module(body = [
Expr(value = Subscript(value = Name(id ='l',ctx = Load())),slice = ExtSlice(dims = [
Slice(下= Num(n = 1),上= Num(n = 2),步长=无),
Index(value = Num(n = 3)) ,
]),ctx = Load())),
])

但是此语法在交互式python shell中不起作用:

 >>> foo = range(10)
>> foo [1:2,3]
追溯(最近一次呼叫最近):
文件< stdin>,在< module>中的第1行。
TypeError:列表索引必须是整数,而不是元组

任何人都知道如何使用



相关讨论:




解决方案

语法在shell中运行良好,只是 list 对象不支持扩展切片。您尝试执行的操作引发了 TypeError ,而不是 SyntaxError



许多 Numpy 数组类型都可以;该项目有助于驱动扩展的切片语法。 Numpy数组使用扩展切片来解决多维数组的不同维度。请参见 Numpy 索引章节了解有关语法用法的详细信息。



扩展切片为明确记录在 Subscription 部分中,AST节点对 extended_slicing 术语进行编码:

  extended_slicing :: =主要 [ slice_list] 
slice_list :: = slice_item(, slice_item )* [,]
slice_item :: =表达式|适当的切片|省略号
proper_slice :: = short_slice | long_slice

但是,Python标准库本身没有使用扩展切片的类型。 / p>

您可以轻松构建自己的类以接受扩展切片;只希望在您的 object .__ getitem__中处理一个元组。 ()方法实现:

 >>类Foo(对象):
... def __getitem __(自身,项目):
...返回项目
...
>> foo = Foo()
>> foo [1,2:3]
(1,slice(2,3,None))

slice_list 的每个元素成为元组中的对象,并以分隔的切片索引作为 slice()实例。


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:

解决方案

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.

这篇关于何时在Python的AST中使用ExtSlice节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆