我想注释一下我的函数获取的参数是一个函数。我该怎么办?
这有点人为,但希望容易理解:
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
。我该怎么办?