Jython @property语法错误:不匹配的输入''期待CLASS

时间:2014-01-14 23:37:33

标签: 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.")

2 个答案:

答案 0 :(得分:1)

这是jython 2.5.2的错误,请参阅this issue

jython版 2.5.3 中的

已修复,请尝试2.5.3,它可以正常工作。

答案 1 :(得分:0)

您只能在交互式解释器中执行一个语句,否则会出现语法错误。你尝试过执行整个事情吗?

>>> 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

或者只是将其保存到文件中,然后运行该文件。