我是一个有用的助手,可以翻译文本。
这些工作得很好。但一旦我为
为什么会发生这种情况,我该如何解决?
我正在尝试使用Python3的类型注释功能。
这里是一些没有注释的玩具函数:
def fa(func, *args):
return func(*args)
def fb(x:str):
return x + " returned."
fa(fb, "Newton")
这些工作得很好。但一旦我为
fa
添加了一些注释,它就出了问题:def fa(func:function, *args):
return func(*args)
def fb(x:str):
return x + " returned."
fa(fb, "Newton")
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-17-193b74f82e47>", line 1, in <module>
def fa(func:function, *args):
NameError: name 'function' is not defined
为什么会发生这种情况,我该如何解决?
str
已经被定义,它是内置的。 - jonrsharpe