简体   繁体   English

为什么在使用 >>= 运算符时会出现语法错误?

[英]Why am I getting a syntax error when using the >>= operator?

Note: I am importing the parsec library , which overloads the >>= (ie - __irshift()__ ) operator .注意:我正在导入parsec,它重载>>= (ie - __irshift()__ ) 运算符

The following Python code:以下 Python 代码:

#! /usr/bin/env python

# irshift_debug.py

from parsec import *

def id(x):
    """Identity parser."""
    return string("").result(x)

testp = digit() >>= id

print(testp.parse("5"))

yields:产量:

$ ./irshift_debug.py
  File "./irshift_debug.py", line 9
    testp = digit() >>= id
                    ^
SyntaxError: invalid syntax

But, if I change the offending line to:但是,如果我将违规行更改为:

testp = digit().bind(id)

then I get:然后我得到:

$ ./irshift_debug.py
5

as expected.正如预期的那样。

I thought that >>= was the infix equivalent of bind() ;我认为>>=bind()的中缀等价物; is that incorrect?这是不正确的吗?

Because >>= is a statement, not an expression.因为>>=是一个语句,而不是一个表达式。 It doesn't evaluate to a result that can be assigned to testp .它不会评估可以分配给testp的结果。

Perhaps you were looking for this?也许你正在寻找这个?

testp = digit >> id

I have used neither Parsec nor Haskell, so I can't speak to the intended use in the context of this library.我既没有使用 Parsec 也没有使用 Haskell,所以我无法在这个库的上下文中谈论预期用途。 However, I believe the main root of your confusion here is that augmented assignment works differently than ordinary operators, and therefore reimplementing them works slightly differently as well.但是,我相信您在这里感到困惑的主要原因是增强赋值的工作方式与普通运算符不同,因此重新实现它们的工作方式也略有不同

So you've seen this code:所以你已经看到了这段代码:

def __irshift__(self, other):
    '''Implements the `(>>=)` operator, means `bind`.'''
    return self.bind(other)

What isn't immediately obvious is that, because this is overriding augmented assignment, not an operator, the returned value is used to reassign the original item on the left.不是很明显的是,因为这是覆盖增强赋值,而不是运算符,所以返回值用于重新分配左侧的原始项目。 In other words, this code:换句话说,这段代码:

a >>= b

is (essentially) equivalent to: (基本上)等同于:

a = a.bind(b)

In either case, as said in Alexander's answer , the resulting assignment is not actually an expression, and ultimately returns no value (not even None ).在任何一种情况下,正如Alexander's answer中所说,生成的赋值实际上不是一个表达式,并且最终不返回任何值(甚至不返回None )。 The right half of an assignment has to be an expression;赋值的右半部分必须是表达式; it can't be another assignment 1 .它不能是另一个任务1 Just as in Python you can't do things like:就像在 Python 中一样,您不能执行以下操作:

a = b = c

So you also can't do:所以你也不能这样做:

a = b >>= c

...even when you've reimplemented >>= . ...即使您重新实现了>>=


1 The only partial exception is the recently added assignment expression , but it is (intentionally) limited in use, and won't help you here. 1唯一的部分例外是最近添加的赋值表达式,但它(故意)使用受限,在这里对您没有帮助。

It is a design mistake.这是一个设计错误。

I have turned to use >= as the operator for bind() and deprecated >>= .我已转而使用>=作为bind()的运算符并弃用了>>= A new release of parsec.py has been upload to pypi.新版本的 parsec.py 已上传到 pypi。

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

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