简体   繁体   English

从python中的模块导入运算符的语法

[英]Syntax to import an operator from a module in python

I'm working with this library , which (very reasonably because it is translating from another language's idioms) makes heavy use of operator redefinition. 我正在使用此库该库 (非常合理,因为它是从另一种语言的习惯用法翻译而来)大量使用了运算符重定义。

Usage begins with from parsec import * which in general, I try to avoid, and rather keep my namespace. 用法from parsec import *开始,一般情况下,我会尽量避免使用,而是保留我的命名空间。 Something more like import parsec as p then using the sub functions and every operator, explicitly. 类似于import parsec as p然后显式使用子函数和每个运算符。

This works for some functions, like p.many1() or p.spaces() or p.regex() . 这适用于某些功能,例如p.many1()p.spaces()p.regex()

However, this also leaves me trying to import name-spaced operators, and that looks even less pythonic. 但是,这也使我尝试导入以名称分隔的运算符,而且看起来还没有Pythonic。

For example, the bitwise >> and << are redefined. 例如,按位>><<被重新定义。 Here is a real-world usage . 这是实际用法

Not only does trying to use a namespace to call these operators also look unpythonic, it's not even clear to me how to do it: p.>> ? 尝试使用名称空间来调用这些运算符不仅看起来像是非Python的,我什至不清楚如何实现: p.>>吗? As you can see, this is a syntax error: 如您所见,这是一个语法错误:

>>> import parsec as p
>>> p.>> p.many()
  File "<stdin>", line 1
    p.>>
       ^
SyntaxError: invalid syntax

This leaves me deciding I want to import the operators implicitly, that is, to call >> without a namespace, but to import the non-operators inside a p namespace. 这让我决定要隐式导入运算符,即在没有名称空间的情况下调用>> ,但要在p名称空间中导入非运算符。

edit update 编辑更新

To be clear, my question is: 明确地说,我的问题是:

How can I import the functions like many1() with a namespace as p.many1(), and import the operator functions like "+" ">>" "<<" nakedly, without a namespace? 如何才能将像many1()这样的函数导入带有p.many1()的命名空间,以及如何在不命名空间的情况下裸露地导入“ +”,“ >>”,“ <<”这样的运算符?

end update 结束更新

While I can do 虽然我可以做

import parsec as p

To get the non-operator functions, it's not clear to me how to say: 要获得非操作员功能,我不清楚如何说:

from parsec import >>

As the following attempts at explicitly importing operators all fail: 由于以下尝试显式导入运算符的尝试均失败了:

>>> from parsec import >>
  File "<stdin>", line 1
    from parsec import >>
                        ^
SyntaxError: invalid syntax
>>> from parsec import (>>)
  File "<stdin>", line 1
    from parsec import (>>)
                         ^
SyntaxError: invalid syntax
>>> from parsec import ">>"
  File "<stdin>", line 1
    from parsec import ">>"
                          ^
SyntaxError: invalid syntax
>>> from parsec import __lshift__, __rshift__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name '__lshift__' from 'parsec' ()

How can I import these? 如何导入这些?

I think the operators are defined as methods on the parser objects, so you don't import them as operators explicitly. 我认为运算符被定义为解析器对象上的方法,因此您不必明确将其作为运算符导入。 If you import the other objects with ap namespace, and you perform the operator operations on them, it will "just work" because the Parser class has these defined. 如果使用ap命名空间导入其他对象,并对它们执行操作符操作,则它将“正常工作”,因为Parser类已定义了这些对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM