Skip to content

Commit a5b3b23

Browse files
authored
doc: Update python.md (#986)
增加高阶函数sorted reduce map以及偏函数,增加类属性的访问控制和验证
1 parent 91d7887 commit a5b3b23

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/python.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,33 @@ print(Yoki.legs) # => 4
13941394
Yoki.sound() # => Woof!
13951395
```
13961396

1397+
### 属性封装与访问控制
1398+
1399+
实现计算属性、只读属性和验证逻辑。
1400+
```python
1401+
class Person:
1402+
def __init__(self, age):
1403+
self._age = age # 约定:_age 为内部属性
1404+
1405+
@property
1406+
def age(self):
1407+
"""获取年龄的方法,伪装成属性"""
1408+
return self._age
1409+
1410+
@age.setter
1411+
def age(self, value):
1412+
"""设置年龄的方法,添加验证逻辑"""
1413+
if value < 0:
1414+
raise ValueError("年龄不能为负数")
1415+
self._age = value
1416+
1417+
# 使用示例
1418+
p = Person(30)
1419+
print(p.age) # 直接访问属性,无需括号 → 30
1420+
p.age = 31 # 赋值操作调用 @age.setter → 验证通过
1421+
p.age = -5 # 抛出 ValueError: 年龄不能为负数
1422+
```
1423+
13971424
Python 数据模型
13981425
--------
13991426

@@ -1813,6 +1840,80 @@ else: # try/except 块的可选子句。 必须遵循除块
18131840
finally: # 在所有情况下执行
18141841
print("我们可以在这里清理资源")
18151842
```
1843+
### 高阶函数map
1844+
1845+
将一个函数应用到可迭代对象(如列表)的每个元素上,并返回一个新的迭代器。
1846+
```python
1847+
def square(x):
1848+
return x ** 2
1849+
1850+
使用 map 函数
1851+
numbers = [1, 2, 3, 4]
1852+
result = map(square, numbers)
1853+
1854+
转换为列表查看结果
1855+
print(list(result)) # 输出: [1, 4, 9, 16]
1856+
```
1857+
1858+
### 高阶函数sorted
1859+
1860+
对可迭代对象进行排序,返回一个新的已排序列表(原对象不变)
1861+
```python
1862+
# 按照分数排序
1863+
users = [
1864+
{"name": "Alice", "score": 95, "time": "2023-01-15 10:30:00"},
1865+
{"name": "Bob", "score": 88, "time": "2023-01-15 09:45:00"},
1866+
{"name": "Charlie", "score": 95, "time": "2023-01-14 15:20:00"},
1867+
{"name": "David", "score": 85, "time": "2023-01-16 11:10:00"}
1868+
]
1869+
# reverse=True代表降序排序
1870+
sorted_users = sorted(users, key=lambda x: x["score"], reverse=True)
1871+
1872+
# 输出结果
1873+
for user in sorted_users:
1874+
print(f"{user['name']}: {user['score']}")
1875+
1876+
# 结果:
1877+
# Alice: 95
1878+
# Charlie: 95
1879+
# Bob: 88
1880+
# David: 85
1881+
```
1882+
1883+
### 高阶函数reduce
1884+
1885+
将一个二元函数(接受两个参数的函数)累积应用到可迭代对象的元素上,最终合并为单个值
1886+
```python
1887+
from functools import reduce
1888+
1889+
# 定义一个乘法函数
1890+
def multiply(x, y):
1891+
return x * y
1892+
1893+
# 使用 reduce 函数
1894+
numbers = [2, 3, 4, 5]
1895+
result = reduce(multiply, numbers)
1896+
1897+
print(result) # 输出: 120(2×3×4×5=120)
1898+
```
1899+
### 偏函数
1900+
1901+
固定原函数的某些参数,生成新函数
1902+
```python
1903+
from functools import partial
1904+
1905+
# 原函数:计算 x 的 y 次幂
1906+
def power(x, y):
1907+
return x ** y
1908+
1909+
# 创建偏函数,固定 y=2(即平方函数)
1910+
square = partial(power, y=2)
1911+
1912+
# 调用偏函数
1913+
print(square(5)) # 输出: 25 (5²)
1914+
print(square(10)) # 输出: 100 (10²)
1915+
```
1916+
18161917

18171918
### pyenv & pipenv
18181919
<!--rehype:wrap-class=col-span-3-->

0 commit comments

Comments
 (0)