如何为函数参数添加类型提示?

时间:2019-12-03 07:36:27

标签: python python-3.6 python-3.7 type-hinting

我想注释一下我的函数获取的参数是一个函数。我该怎么办?

示例

这有点人为,但希望容易理解:

from typing import List

def square_some_numbers(number_sequence: List[int], number_selector) -> List[int]:
    return [nr**2 if number_selector(nr) else nr for nr in number_sequence]

number_sequence = list(range(20))
number_selector = lambda n: n % 2 == 0
square_some_numbers(number_sequence, number_selector)

礼物:

[0, 1, 4, 3, 16, 5, 36, 7, 64, 9, 100, 11, 144, 13, 196, 15, 256, 17, 324, 19]

我想注释一下number_selector是一个函数,该函数接收一个类型为int的参数并返回一个bool。我该怎么办?

0 个答案:

没有答案