Skip to main content
 首页 » 编程设计

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

2025年05月04日7itcoder

我尝试从 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,它可以工作。