python - Jython @property 语法错误 : mismatched input '' expecting CLASS

标签 python syntax-error decorator jython python-decorators

我尝试从 Jython 解释器中的文档运行这个示例:

http://www.jython.org/docs/library/functions.html

class C(object):
    def __init__(self):
        self._x = None
    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x
    @x.setter
    def x(self, value):
        self._x = value
    @x.deleter
    def x(self):
        del self._x

只需输入前 4 行(直到并包括 @property)就会产生 SyntaxError:

>>> class C(object):
...     def __init__(self):
...         self._x = None
...     @property
  File "<stdin>", line 4
    @property
            ^
SyntaxError: mismatched input '' expecting CLASS

更新:我使用的是 Jython 2.5.2

这是我粘贴整个内容时发生的情况:

$ jython
Jython 2.5.2 (Debian:hg/91332231a448, Jun 3 2012, 09:02:34) 
[Java HotSpot(TM) 64-Bit Server VM (Sun Microsystems Inc.)] on java1.6.0_45
Type "help", "copyright", "credits" or "license" for more information.
>>> class C(object):
...     def __init__(self):
...         self._x = None
...     @property
  File "<stdin>", line 4
    @property
            ^
SyntaxError: mismatched input '' expecting CLASS
>>>     def x(self):
  File "<stdin>", line 1
    def x(self):
    ^
SyntaxError: no viable alternative at input '    '
>>>         """I'm the 'x' property."""
  File "<stdin>", line 1
    """I'm the 'x' property."""
    ^
SyntaxError: no viable alternative at input '        '
>>>         return self._x
  File "<stdin>", line 1
    return self._x
    ^
SyntaxError: no viable alternative at input '        '
>>>     @x.setter
  File "<stdin>", line 1
    @x.setter
    ^
SyntaxError: no viable alternative at input '    '
>>>     def x(self, value):
  File "<stdin>", line 1
    def x(self, value):
    ^
SyntaxError: no viable alternative at input '    '
>>>         self._x = value
  File "<stdin>", line 1
    self._x = value
    ^
SyntaxError: no viable alternative at input '        '
>>>     @x.deleter
  File "<stdin>", line 1
    @x.deleter
    ^
SyntaxError: no viable alternative at input '    '
>>>     def x(self):
  File "<stdin>", line 1
    def x(self):
    ^
SyntaxError: no viable alternative at input '    '
>>>         del self._x
  File "<stdin>", line 1
    del self._x
    ^
SyntaxError: no viable alternative at input '        '
>>> 

更新 2:谢谢!

对于可以控制哪个 Jython 版本的人,请升级到 2.5.3。对于那些无法控制它的人,请使用没有装饰器的旧式语法:

class C(object):
    def __init__(self):
        self._x = None
    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x
    x = property(getx, setx, delx, "I'm the 'x' property.")

最佳答案

这是一个 jython 2.5.2 的错误,参见 this issue .

已修复在 jython 版本 2.5.3 中,尝试 2.5.3,它可以工作。

关于python - Jython @property 语法错误 : mismatched input '' expecting CLASS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21126347/

相关文章:

python - 操作系统错误 : [WinError 10022] An invalid argument was supplied - Windows 10 Python

python - python中的装饰器与在函数上调用函数完全一样吗?

python - 语法错误: invalid syntax line 138 unexpected error

python - 访问列出的元组中的元素,在我的函数中创建错误

objective-c - 在 linux 中编译目标代码错误 (.text+0x20) : undefined reference to `main'

Python @property 装饰器不工作

python - 类装饰器与函数装饰器

python - Python 在标准库中有装饰器吗?

python - 带有 ListDirectory 的 Tensorflow 数据集 API

python - 将 Python 应用程序压缩到单个源文件中