Skip to content

Commit d94fa43

Browse files
committed
chatch-up post for Sunday yesterday: python function parameters
1 parent a354532 commit d94fa43

File tree

13 files changed

+608
-33
lines changed

13 files changed

+608
-33
lines changed

content/posts/fastapi-cookie-header-parameters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ X-Token: bar
144144
```
145145

146146
返回类似这样
147-
```Python
147+
```JSON
148148
{
149149
"X-Token values": [
150150
"bar",
@@ -202,7 +202,7 @@ tool: plumbus
202202
}
203203
```
204204

205-
#### Disable convert undersocres
205+
#### Disable convert undersocres 禁止转换下划线
206206
同样可以禁用自动下换线转换
207207

208208
与普通的 Header 参数一样, 如果参数名中包含下划线 undersocre (\_), FastAPI 会自动将其转换为连字符 hypens (-)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
+++
2+
date = '2025-08-10T10:00:00+08:00'
3+
draft = false
4+
title = 'Python Function Parameters'
5+
+++
6+
今天是周日, 简单写点吧, 简单总结一下 Python 中函数参数
7+
8+
## Python Function Parameters
9+
Python 函数参数机制非常灵活丰富, 理解各种参数类型及其用法对于写出优雅、易维护的代码非常重要. 本文将介绍 Python 中函数参数的种类与用法, 并详细讲解 Python 3.8 引入的参数分隔符 / 和 *, 帮助你更好地设计函数接口.
10+
11+
#### 1. Postional Arguments 位置参数
12+
函数定义中最常见的参数, 调用时按顺序传入值
13+
```Python
14+
def greet(name, age):
15+
print(f"Hello, {name}. You are {age} years old.")
16+
17+
greet("Alice", 30) # Hello, Alice. You are 30 years old.
18+
```
19+
20+
#### 2. Keyword Arguments 关键字参数
21+
调用时以 `key=value` 形式传入, 顺序可变
22+
```Python
23+
greet(age=30, name="Alice")
24+
```
25+
26+
#### 3. Default Arguments 默认参数
27+
定义函数时给参数赋默认值, 调用时可省略
28+
```Python
29+
def greet(name, age=20):
30+
print(f"Hello, {name}. You are {age} years old.")
31+
32+
greet("Bob") # 使用默认年龄20
33+
greet("Bob", 25) # 指定年龄
34+
```
35+
36+
- 注意: 使用默认参数尽量不要使用可变类型(mutable), 例如列表, 因为默认参数是存储在函数中的, 而非函数实例中, 多次调用会改变默认值的内容.
37+
38+
```Python
39+
def greet(names: list[str] = ["Alice", "Bob"]):
40+
...
41+
```
42+
43+
若希望使用默认值, 建议使用下面这种方法
44+
```Python
45+
def greet(names: list[str] | None = None):
46+
if not names:
47+
names = ["Alice", "Bob"]
48+
...
49+
```
50+
51+
同样的, 默认值参数如果为一个表达式, 则是在定义时求值, 而非运行改函数时才求值
52+
53+
54+
55+
56+
#### 4. `*args` 可变位置参数
57+
用于接收任意数量的位置参数, 形成元组
58+
```Python
59+
def sum_all(*args):
60+
return sum(args)
61+
62+
sum_all(1, 2, 3) # 6
63+
sum_all() # 0
64+
```
65+
66+
#### 5. `**kwargs` 可变关键字参数
67+
用于接收任意数量的关键字参数, 形成字典
68+
```Python
69+
def print_info(**kwargs):
70+
for k, v in kwargs.items():
71+
print(f"{k} = {v}")
72+
73+
print_info(name="Alice", age=30)
74+
```
75+
76+
### / 和 * 的用法
77+
Python 3.8 引入了两种新的函数参数分隔符: 斜杠 `/`(forward slash) 和 星号 `*`(asterisk) 符号, 用于更精细地控制参数的调用方式
78+
79+
#### Postional-only parameters (`/`)
80+
斜杠前的参数必须通过位置传递, 不能用关键字传递
81+
```Python
82+
def func(a, b, /, c, d):
83+
print(a, b, c, d)
84+
```
85+
86+
调用时
87+
```Python
88+
func(1, 2, c=3, d=4) # 正确
89+
func(1, 2, 3, 4) # 也正确
90+
func(a=1, b=2, c=3, d=4) # 错误,a 和 b 不能用关键字传递
91+
```
92+
93+
用途:
94+
- 保护函数接口的参数顺序, 避免调用者用关键字修改参数值
95+
- 兼容一些C语言扩展模块的调用约定
96+
- 明确哪些参数是"位置专用"的
97+
98+
99+
#### Keyword-only parameters (`*`)
100+
星号后的参数必须用关键字传递, 不能用位置传递
101+
```Python
102+
def func(a, b, *, c, d):
103+
print(a, b, c, d)
104+
```
105+
106+
调用时
107+
```Python
108+
func(1, 2, c=3, d=4) # 正确
109+
func(1, 2, 3, 4) # 错误,c 和 d 只能用关键字传递
110+
```
111+
112+
用途:
113+
- 强制调用者明确指定关键字参数, 提高代码可读性
114+
- 避免参数顺序引起的混淆
115+
116+
#### Use both `/` and `*`
117+
`/``*` 也可以同时使用
118+
```Python
119+
def func(a, b, /, c, d, *, e, f):
120+
print(a, b, c, d, e, f)
121+
```
122+
123+
调用时
124+
- a 和 b 只能用位置参数传递
125+
- c 和 d 都可以
126+
- e 和 f 只能用关键字参数传递

public/archives/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@
5151
<span class="max-w-[4rem] md:max-w-none truncate">Home</span></a></li><li class="flex items-center gap-1 md:gap-2 min-w-0"><span class="text-muted-foreground/50 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
5252
</span><span class="text-foreground flex items-center gap-0.5 md:gap-1 font-medium min-w-0 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg>
5353
<span class="max-w-[3rem] md:max-w-none truncate">Archives</span></span></li></ol></nav><header class=mb-8><div class="mb-4 flex items-center gap-3"><svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg><h1 class="text-foreground text-3xl font-bold">Archives</h1></div><p class="text-muted-foreground mb-6">Browse all articles in chronological order and discover what interests you.</p><div class="text-muted-foreground flex items-center gap-4 text-sm"><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
54-
<span>9 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55-
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">9
56-
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">9
54+
<span>10 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55+
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">10
56+
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">10
5757
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/fastapi-cookie-and-header-parameters/ class=block>Fastapi Cookie and Header Parameters</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
5858
<time datetime=2025-08-11>08-11</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
5959
<span>3
60+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/python-function-parameters/ class=block>Python Function Parameters</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
61+
<time datetime=2025-08-10>08-10</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
62+
<span>2
6063
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/fastapi-body-advanced-uses/ class=block>FastAPI Body Advanced Uses</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
6164
<time datetime=2025-08-09>08-09</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
6265
<span>5

public/en/sitemap.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="utf-8" standalone="yes"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"><url><loc>https://starslayerx.github.io/posts/fastapi-cookie-and-header-parameters/</loc><lastmod>2025-08-11T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/</loc><lastmod>2025-08-11T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/"/></url><url><loc>https://starslayerx.github.io/posts/fastapi-body-advanced-uses/</loc><lastmod>2025-08-09T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/git-whitelist/</loc><lastmod>2025-08-08T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters-and-validations/</loc><lastmod>2025-08-07T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters/</loc><lastmod>2025-08-06T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/python-tricks/</loc><lastmod>2025-08-05T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/executing-arbitrary-python-code-from-a-comment/</loc><lastmod>2025-08-04T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/how-fastapi-works/</loc><lastmod>2025-08-01T10:30:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/blaugust/</loc><lastmod>2025-08-01T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/blaugust/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/blaugust/"/></url><url><loc>https://starslayerx.github.io/</loc><lastmod>2023-01-01T08:00:00-07:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/"/></url><url><loc>https://starslayerx.github.io/archives/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/archives/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/archives/"/></url><url><loc>https://starslayerx.github.io/categories/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/categories/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/categories/"/></url><url><loc>https://starslayerx.github.io/tags/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/tags/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/tags/"/></url></urlset>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"><url><loc>https://starslayerx.github.io/posts/fastapi-cookie-and-header-parameters/</loc><lastmod>2025-08-11T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/</loc><lastmod>2025-08-11T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/"/></url><url><loc>https://starslayerx.github.io/posts/python-function-parameters/</loc><lastmod>2025-08-10T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-body-advanced-uses/</loc><lastmod>2025-08-09T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/git-whitelist/</loc><lastmod>2025-08-08T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters-and-validations/</loc><lastmod>2025-08-07T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters/</loc><lastmod>2025-08-06T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/python-tricks/</loc><lastmod>2025-08-05T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/executing-arbitrary-python-code-from-a-comment/</loc><lastmod>2025-08-04T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/how-fastapi-works/</loc><lastmod>2025-08-01T10:30:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/blaugust/</loc><lastmod>2025-08-01T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/blaugust/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/blaugust/"/></url><url><loc>https://starslayerx.github.io/</loc><lastmod>2023-01-01T08:00:00-07:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/"/></url><url><loc>https://starslayerx.github.io/archives/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/archives/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/archives/"/></url><url><loc>https://starslayerx.github.io/categories/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/categories/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/categories/"/></url><url><loc>https://starslayerx.github.io/tags/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/tags/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/tags/"/></url></urlset>

0 commit comments

Comments
 (0)