Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
toCodeOnLogicAndMath
explore-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
explore-python
/
Functional
/
partial.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
87 lines (59 loc) · 2.04 KB
master
Breadcrumbs
explore-python
/
Functional
/
partial.md
Copy path
Top
File metadata and controls
Code
Blame
87 lines (59 loc) · 2.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# partial 函数
Python
提供了一个
functools
的模块
,
该模块为高阶函数提供支持
,
partial
就是其中的一个函数
,
该函数的形式如下
:
``
`
python
functools
.
partial
(
func
[,
*
args
][,
**
kwargs
])
``
`
这里先举个例子
,
看看它是怎么用的
。
假设有如下函数
:
``
`
python
def
multiply
(
x
,
y
):
return
x
*
y
``
`
现在
,
我们想返回某个数的双倍
,
即
:
``
`
>>
>
multiply
(
3
,
y
=
2
)
6
>
>>
multiply
(
4
,
y
=
2
)
8
>
>>
multiply
(
5
,
y
=
2
)
10
``
`
上面的调用有点繁琐
,
每次都要传入
`y=2`
,
我们想到可以定义一个新的函数
,
把
`y=2`
作为默认值
,
即
:
``
`
python
def
double
(
x
,
y
=
2
):
return
multiply
(
x
,
y
)
``
`
现在
,
我们可以这样调用了
:
``
`
>>
>
double
(
3
)
6
>
>>
double
(
4
)
8
>
>>
double
(
5
)
10
``
`
事实上
,
我们可以不用自己定义
`double`
,
利用
`partial`
,
我们可以这样
:
``
`
python
from
functools
import
partial
double
=
partial
(
multiply
,
y
=
2
)
``
`
`partial`
接收函数
`multiply`
作为参数
,
固定
`multiply`
的参数
`y=2`
,
并返回一个新的函数给
`double`
,
这跟我们自己定义
`double`
函数的效果是一样的
。
所以
,
简单而言
,
`partial`
函数的功能就是
:
把一个函数的某些参数给固定住
,
返回一个新的函数
。
需要注意的是
,
我们上面是固定了
`multiply`
的关键字参数
`y=2`
,
如果直接使用
:
``
`
python
double
=
partial
(
multiply
,
2
)
``
`
则
`2`
是赋给了
`multiply`
最左边的参数
`x`
,
不信
?
我们可以验证一下
:
``
`
python
from
functools
import
partial
def
subtraction
(
x
,
y
):
return
x
-
y
f
=
partial
(
subtraction
,
4
)
# 4 赋给了 x
>
>>
f
(
10
)
# 4 - 10
-
6
``
`
# 小结
-
partial
的功能
:
固定函数参数
,
返回一个新的函数
。
-
当函数参数太多
,
需要固定某些参数时
,
可以使用
`functools.partial`
创建一个新的函数
。
# 参考资料
-
[
偏函数
-
廖雪峰的官方网站
](
http
:
//
www
.
liaoxuefeng
.
com
/
wiki
/
001374738125095
c955c1e6d8bb493182103fac9270762a000
/
001386819893624
a7edc0e3e3df4d5d852a352b037c93ec000
)
You can’t perform that action at this time.