Python 3: "NameError: name 'function' is not defined" -
running
def foo(bar: function): bar() foo(lambda: print("greetings lambda."))
with python 3.6.2 yields
traceback (most recent call last): file "<stdin>", line 1, in <module> nameerror: name 'function' not defined
however, removing type annotation works expected.
pycharm additionally gives warning 'function' object not callable
on line bar()
.
edit: stated in comment of pieters’ answer, question raised, because
def myfunction(): pass print(myfunction.__class__)
outputs <class 'function'>
.
there no name function
defined in python, no. annotations still python expressions , must reference valid names.
you can instead use type hinting bar
callable; use typing.callable
:
from typing import callable def foo(bar: callable[[], any]): bar()
this defines callable type takes no arguments , return value can (we don't care).
Comments
Post a Comment