Skip to content

Commit 4fdff89

Browse files
偏函数的学习
1 parent 9a73f47 commit 4fdff89

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1. 写一篇文章,详细的介绍lambda
1+
1. 写一篇文章,详细的介绍lambda
2+
2.如果有精力的话建议学习《Learn Python the Hard Way》这本书

day-11/partial-func.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# 使用functools.partial创建偏函数
3+
print(int('123')) # 123
4+
print(int('123', base=8)) # 83
5+
6+
# 固定函数的部分参数
7+
def int_base_2(x, base=2):
8+
return int(x, base)
9+
10+
print(int_base_2('1010110')) # 86
11+
12+
# 使用functools
13+
import functools
14+
15+
int_base_2_v2 = functools.partial(int, base=2)
16+
17+
print(int_base_2_v2('1010110')) # 86
18+
19+
max_10 = functools.partial(max, 10)
20+
print(max_10(2, 3, 4)) # 10

0 commit comments

Comments
 (0)