mirror of
https://github.com/jaywcjlove/reference.git
synced 2026-04-05 15:39:05 +08:00
doc: format markdown.
This commit is contained in:
@@ -1397,6 +1397,7 @@ Yoki.sound() # => Woof!
|
||||
### 属性封装与访问控制
|
||||
|
||||
实现计算属性、只读属性和验证逻辑。
|
||||
|
||||
```python
|
||||
class Person:
|
||||
def __init__(self, age):
|
||||
@@ -1840,9 +1841,11 @@ else: # try/except 块的可选子句。 必须遵循除块
|
||||
finally: # 在所有情况下执行
|
||||
print("我们可以在这里清理资源")
|
||||
```
|
||||
|
||||
### 高阶函数map
|
||||
|
||||
将一个函数应用到可迭代对象(如列表)的每个元素上,并返回一个新的迭代器。
|
||||
|
||||
```python
|
||||
def square(x):
|
||||
return x ** 2
|
||||
@@ -1858,6 +1861,7 @@ print(list(result)) # 输出: [1, 4, 9, 16]
|
||||
### 高阶函数sorted
|
||||
|
||||
对可迭代对象进行排序,返回一个新的已排序列表(原对象不变)
|
||||
|
||||
```python
|
||||
# 按照分数排序
|
||||
users = [
|
||||
@@ -1883,6 +1887,7 @@ for user in sorted_users:
|
||||
### 高阶函数reduce
|
||||
|
||||
将一个二元函数(接受两个参数的函数)累积应用到可迭代对象的元素上,最终合并为单个值
|
||||
|
||||
```python
|
||||
from functools import reduce
|
||||
|
||||
@@ -1896,9 +1901,11 @@ result = reduce(multiply, numbers)
|
||||
|
||||
print(result) # 输出: 120(2×3×4×5=120)
|
||||
```
|
||||
|
||||
### 偏函数
|
||||
|
||||
固定原函数的某些参数,生成新函数
|
||||
|
||||
```python
|
||||
from functools import partial
|
||||
|
||||
@@ -1914,7 +1921,6 @@ print(square(5)) # 输出: 25 (5²)
|
||||
print(square(10)) # 输出: 100 (10²)
|
||||
```
|
||||
|
||||
|
||||
### pyenv & pipenv
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
|
||||
|
||||
Reference in New Issue
Block a user