-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.py
64 lines (57 loc) · 1.47 KB
/
14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#
# Copyright: (C) 2023 shachi All rights reserved.
#
# File: .\src\14.py
# Author: shachi <[email protected]>
# Description: 返回函数
#
from typing import Callable
def lazy_sum(*args: int) -> Callable[[],int] :
def sum() -> int:
ax = 0
for n in args:
ax: int= ax + n
return ax
return sum
def count() -> list[Callable[[],int]]:
fs: list[Callable[[],int]] = []
for i in range(1, 4):
def f() -> int:
return i * i
fs.append(f)
return fs
def my_count() -> list[Callable[[],int]]:
def f(j)-> Callable[[],int]:
def g()-> int:
return j*j
return g
fs:list[Callable[[],int]] = []
for i in range(1, 4):
fs.append(f(i)) # f(i)立刻被执行,因此i的当前值被传入f()
return fs
def inc() -> Callable[[], int]:
x: int = 0
def fn() -> int:
nonlocal x
x = x + 1
return x
return fn
def createCounter() -> Callable[[], int]:
x: int = 0
def counter() -> int:
nonlocal x
x += 1
return x
return counter
def main() -> None:
# func: Callable[[],int] = lazy_sum(1, 3, 5, 7, 9)
# print(func())
func1: Callable[[], int] = createCounter()
print(func1(), func1(), func1())
# * lambda 函数
L: list[int] = list(filter(lambda x: x % 2 == 1,range(1, 20)))
print(L)
if __name__ == '__main__':
main()