现在的位置: 首页 > 问答 > 正文

Python 3: "NameError: name 'function' is not defined"

2017年09月14日 问答 ⁄ 共 454字 ⁄ 字号 暂无评论

Running

def foo(bar: function):
    bar()

foo(lambda: print("Greetings from lambda."))

with Python 3.6.2 yields

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined

However, removing the type annotation works as expected.

PyCharm additionally gives the warning 'function' object is not callable on line bar().


edit: As stated in my comment of Pieters’ answer, this question raised, because

def myfunction():
    pass

print(myfunction.__class__)

outputs <class 'function'>.

给我留言

您必须 [ 登录 ] 才能发表留言!

×