我尝试使用 ipython 使用类方法装饰器创建类方法。 当我按 Enter 键时,出现以下错误:
我尝试在普通的 python 脚本中使用相同的装饰器并且它有效。为什么我不能在 Ipython 中做同样的事情?
将您的
ipython
软件包升级到最新版本,例如
# python3 -m pip install -U ipython
它适用于
ipython==8.1.0
(2022 年 2 月 25 日发布)或更高版本:
$ ipython
Python 3.11.4 (main, Jun 20 2023, 16:52:35) [Clang 13.0.0 (clang-1300.0.29.30)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: class Test:
...: @classmethod
...: def test():
...: pass
...:
In [2]:
我可以为之前版本的
ipython
、8.0.1
重现它:
$ ipython
Python 3.11.4 (main, Jun 20 2023, 16:52:35) [Clang 13.0.0 (clang-1300.0.29.30)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.0.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: class Test:
...: @classmethod
Input In [1]
@classmethod
^
SyntaxError: incomplete input
classmethod
是一个装饰器,你可以将它放在方法之上
class SomeClass:
@classmethod