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
/
anonymous_func.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
74 lines (49 loc) · 1.96 KB
master
Breadcrumbs
explore-python
/
Functional
/
anonymous_func.md
Copy path
Top
File metadata and controls
Code
Blame
74 lines (49 loc) · 1.96 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
#
匿名函数
在 Python 中,我们使用
`
def
`
语句来定义函数,比如:
```
python
def
double
(
x
):
return
2
*
x
```
除了用上面的方式定义函数,Python 还提供了一个关键字
`
lambda
`
,让我们可以创建一个匿名函数,也就是没有名称的函数。它的形式如下:
```
lambda 参数: 表达式
```
关键字
`
lambda
`
说明它是一个匿名函数,冒号
`
:
`
前面的变量是该匿名函数的参数,冒号后面是函数的返回值,注意这里不需使用 return 关键字。
我们将上面的
`
double
`
函数改写成一个匿名函数,如下:
```
lambda x: 2 * x
```
那怎么调用匿名函数呢?可以直接这样使用:
```
python
>>
>
(
lambda
x
:
2
*
x)(
8
)
16
```
由于匿名函数本质上是一个函数对象,也可以将其赋值给另一个变量,再由该变量来调用函数,如下:
```
python
>>
>
f
=
lambda
x
:
2
*
x
#
将匿名函数赋给变量 f
>>
>
f
<
function
<
lambda
> at 0x7f835a696578>
>>
>
f(
8
)
16
```
#
使用场景
`
lambda
`
函数一般适用于创建一些临时性的,小巧的函数。比如上面的
`
double
`
函数,我们当然可以使用
`
def
`
来定义,但使用
`
lambda
`
来创建会显得很简洁,尤其是在高阶函数的使用中。
看一个例子:
```
python
def
func
(
g
,
arr
):
return
[g(x)
for
x
in
arr]
```
现在给一个列表
[
1, 2, 3, 4
]
,利用上面的函数,对列表中的元素加 1,返回一个新的列表,你可能这样用:
```
python
def
add_one
(
x
):
return
x
+
1
arr
=
func(add_one, [
1
,
2
,
3
,
4
])
```
这样做没什么错,可是
`
add_one
`
这个函数太简单了,使用
`
def
`
定义未免有点小题大作,我们改用
`
lambda
`
:
```
arr = func(lambda x: x + 1, [1, 2, 3, 4])
```
是不是很简洁、易懂?
#
小结
-
匿名函数本质上是一个函数,没有函数名称,因此使用匿名函数不用担心函数名冲突;
-
匿名函数一般适用于创建一些临时性的,小巧的函数;
You can’t perform that action at this time.