Skip to content

Commit 123d3c9

Browse files
authored
Patch: Add ** syntax description for functions (#21)
1 parent f4a52d1 commit 123d3c9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/definition-and-invocation-of-functions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ some_fn('position_arg_value','add_to_args1','add_to_args2',a=1,b=2,c=3)
6262
# ('add_to_args1', 'add_to_args2')
6363
# {'a': 1, 'b': 2, 'c': 3}
6464
```
65+
Similarly, Python can expand a dictionary using `**`.
66+
67+
```
68+
def some_fn(a, b, c):
69+
print(a, b, c) # 1 2 3
70+
71+
my_dict = {"c": 3, "b": 2, "a": 1} # Python has no requirement for the order of keys in a dictionary
72+
some_fn(**my_dict)
73+
74+
```
75+
6576
While `*args` and `**kwargs` are convenient, if a function only has these parameters, IDE code suggestions will be lost, and the logic of the function may become harder to understand. Therefore, use them with caution.
6677
:::
6778

i18n/zh-cn/docusaurus-plugin-content-docs/current/definition-and-invocation-of-functions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ some_fn('position_arg_value','add_to_args1','add_to_args2',a=1,b=2,c=3)
6262
# ('add_to_args1', 'add_to_args2')
6363
# {'a': 1, 'b': 2, 'c': 3}
6464
```
65+
类似地,Python 也可以使用 `**` 展开一个字典
66+
67+
```
68+
def some_fn(a, b, c):
69+
print(a, b, c) # 1 2 3
70+
71+
my_dict = {"c": 3, "b": 2, "a": 1} # Python 对字典中键的顺序没有要求
72+
some_fn(**my_dict)
73+
74+
```
75+
76+
6577
`*args``**kwargs` 虽然方便,但如果函数只定义了这两个形参,IDE 将失去代码提示,同时函数的逻辑也将变得难以理解,需要谨慎使用。
6678
:::
6779

0 commit comments

Comments
 (0)