Pyparsing 文档展示了如何使用 setResultsName(...) 方法来设置生成的字典键。但是,我几次看到这个方法被省略了,只使用了 (...) 。 我的意思是:
代替:
exp1 = pp.Word(pp.alphas).setResultsName('word1')
exp1.parseString( "Hello" ).asDict()
... 可以做到:
exp1 = pp.Word(pp.alphas)('word1')
exp1.parseString( "Hello" ).asDict()
...结果将完全相同。
所以 2 个问题:
我在哪里可以找到确认可以完成省略 setResultsName 的文档?
这是如何运作的? pp.Word(pp.alphas) 是一个 pyparsing 对象,是一个函数吗?那又是如何实现的呢?
谢谢
请您参考如下方法:
您可以找到 pyparsing 文档 online .
setResultsName 的文档说:
Define name for referencing matching tokens as a nested attribute of the returned parse results. NOTE: this returns a copy of the original ParserElement object; this is so that the client can define a basic element, such as an integer, and reference it in multiple places with different names.
You can also set results names using the abbreviated syntax, expr("name") in place of expr.setResultsName("name") - see
__call__.
__call__ 的信息证实了这种行为。
由于 ParserElement 类实现了 __call__,这使得该类的实例可调用,就像函数一样。在这种情况下,使用字符串参数调用 ParserElement 实例等同于调用 setResultsName。
