Article Outline

# 化名与匿名

## 化名

``````def _is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

year_leap_bool = _is_leap
print(year_leap_bool)              #<function __main__._is_leap(year)>
print(year_leap_bool(800))         # _is_leap(800) -> True

print(id(year_leap_bool))          # id() 这个函数可以查询某对象的内存地址
print(id(_is_leap))                # year_leap_bool 和 _is_leap 其实保存在同一个地址中，也就是说，它们是同一个对象。

print(type(year_leap_bool))
print(type(_is_leap))              # 它们都是 function``````
``````<function __main__._is_leap(year)>
True
4547071648
4547071648
function
function``````

## lambda

``````def add(x, y):
return x + y
``8``

``````add = lambda x, y: x + y
``8``

lambda 的语法结构如下：

`lambda_expr ::= "lambda" [parameter_list] ":" expression`

``lambda x, y: x + y``

`add = lambda x, y: x + y`

## lambda 的使用场景

### 作为某函数的返回值

``````def make_incrementor(n):
return lambda x: x + n

f = make_incrementor(42)
print(f(0))
print(f(1))``````
``````42
43``````

``````def make_incrementor(n):
return lambda x: x + n

f = make_incrementor(42)
print(f)

print(id(make_incrementor))
print(id(f))``````
``````<function __main__.make_incrementor.<locals>.<lambda>(x)>
4428443296
4428726888``````

``f = make_incrementor``

`f` 是什么呢？它是 `<function __main__.make_incrementor.<locals>.<lambda>(x)>`

• `f = make_incrementor(42)` 是将 `make_incrementor(42)` 的返回值保存到 `f` 这个变量之中；
• `make_incrementor()` 这个函数接收到 `42` 这个参数之后，返回了一个函数：`lambda x: x + 42`
• 于是，`f` 中保存的函数是 `lambda x: x + 42`
• 所以，`f(0)` 是向这个匿名函数传递了 `0`，而后，它返回的是 `0 + 42`

### 作为某函数的参数

`map`(function, iterable, ...)

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see `itertools.starmap()`.

`map()` 这个函数的第一个参数，就是用来接收函数的。随后的参数，是 `iterable` —— 就是可被迭代的对象，比如，各种容器，例如：列表、元组、字典什么的。

``````def double_it(n):
return n * 2

a_list = [1, 2, 3, 4, 5, 6]

b_list = list(map(double_it, a_list))
print(b_list)

c_list = list(map(lambda x: x * 2, a_list))
print(c_list)``````
``````[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8, 10, 12]``````

``````phonebook = [
{
'name': 'john',
'phone': 9876
},
{
'name': 'mike',
'phone': 5603
},
{
'name': 'stan',
'phone': 6898
},
{
'name': 'eric',
'phone': 7898
}
]

print(phonebook)
print(list(map(lambda x: x['name'], phonebook)))
print(list(map(lambda x: x['phone'], phonebook)))``````
``````[{'name': 'john', 'phone': 9876},
{'name': 'mike', 'phone': 5603},
{'name': 'stan', 'phone': 6898},
{'name': 'eric', 'phone': 7898}]

['john', 'mike', 'stan', 'eric']

[9876, 5603, 6898, 7898]``````

``````a_list = [1, 3, 5]
b_list = [2, 4, 6]

print(list(map(lambda x, y: x * y, a_list, b_list)))``````
``[2, 12, 30]``

``````pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda p: p[1])
print(pairs)``````
``[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]``