Python 3 文档 - 函数注释
Python 3 Documentation - Function Annotation
我使用以下格式记录我的 Python 代码:
def my_function(param: str) -> dict:
some code
我不知道如何记录传递给另一个函数的函数。
例如:
def my_function(my_other_function: ???) -> dict:
some code
如何做函数注解?
First Thoughts: "Everything in python is an object"
我在文档中找不到任何内容,但作为 everything in python is an object
我会争取 object
。
def my_function(my_other_function: object) -> dict:
some code
证明:
if isinstance(my_function, my_function, object):
print("yes")
>yes
无论如何,这可能不是太明确,因此:
Seconds thoughts: Using proper type hints
根据 COLDSPEED
的评论,更明确的类型提示将使用 typing
import typing
def my_function(my_other_function:typing.Callable):->dict:
pass
"The only way that annotations take on meaning is when they are interpreted by third-party libraries". Which means, for your source-code itself, it doesn't change anything. Just wanted to mention it.
我使用以下格式记录我的 Python 代码:
def my_function(param: str) -> dict:
some code
我不知道如何记录传递给另一个函数的函数。
例如:
def my_function(my_other_function: ???) -> dict:
some code
如何做函数注解?
First Thoughts: "Everything in python is an object"
我在文档中找不到任何内容,但作为 everything in python is an object
我会争取 object
。
def my_function(my_other_function: object) -> dict:
some code
证明:
if isinstance(my_function, my_function, object):
print("yes")
>yes
无论如何,这可能不是太明确,因此:
Seconds thoughts: Using proper type hints
根据 COLDSPEED
的评论,更明确的类型提示将使用 typing
import typing
def my_function(my_other_function:typing.Callable):->dict:
pass
"The only way that annotations take on meaning is when they are interpreted by third-party libraries". Which means, for your source-code itself, it doesn't change anything. Just wanted to mention it.