Skip to content

Commit 2781b46

Browse files
🗑️ 更新大模型应用开发的核心概念
1 parent cc521d2 commit 2781b46

9 files changed

Lines changed: 802 additions & 477 deletions

File tree

docs/docs/Python/100机器学习.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,3 +2094,6 @@ LSTM(长短期记忆网络)是深度学习中最重要的架构之一。1997
20942094
《An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale》在 2020 年及之后发表的论文中,谷歌学术总引用次数排名第1。
20952095
:::
20962096

2097+
## 推荐资源
2098+
2099+
[李沐动手学深度学习](https://github.com/d2l-ai/d2l-zh)

docs/docs/Python/20类与对象.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,29 @@ print(process_point(Point(99, 0, 0))) # 彩蛋
10421042
print(process_point(Point(100, 0, 0))) # TypeError: Point() accepts 1 positional sub-pattern (3 given)
10431043
```
10441044

1045+
### \_\_fspath\_\_
1046+
1047+
PEP 519 规定,只要一个类实现了 `__fspath__()` 方法,并返回一个字符串或字节串,它就是一个 "Path-like object"(类路径对象)。
1048+
1049+
底层的 C 函数(比如 os.stat)和大量的库过去只认 `str``bytes`。现在,它们会寻找该对象是否有 `__fspath__()` 方法,如果有就用`os.fspath()`来获取路径字符串。
1050+
1051+
通过这样的方式,老代码可以使用新的路径对象,新代码可以更加灵活的对路径进行操作。
1052+
1053+
```python showLineNumbers
1054+
class SmartPath:
1055+
def __init__(self, raw_path):
1056+
self.raw = raw_path
1057+
1058+
def __fspath__(self):
1059+
# 将路径中环境变量的占位符替换为实际值
1060+
return os.path.expandvars(self.raw)
1061+
1062+
# 传入一个字符串:环境变量 $USER_DATA 拼接 /config.json
1063+
p = SmartPath("$USER_DATA/config.json")
1064+
with open(p) as f: # open() 会自动调用 __fspath__ 解析出真实路径
1065+
pass
1066+
```
1067+
10451068
## 重载类型转换
10461069

10471070
### \_\_repr\_\_\_\_str\_\_

0 commit comments

Comments
 (0)