mirror of
https://github.com/leisurelicht/wtfpython-cn.git
synced 2024-12-06 16:30:12 +08:00
EX.First things first
This commit is contained in:
parent
377de780d8
commit
abb8843a1f
@ -1 +1,3 @@
|
|||||||
* 2018-12-16 更新至[30e05a5](https://github.com/satwikkansal/wtfpython/commit/30e05a5973930c38cdb59f1c02b85b19b22ac531)
|
* 2018-12-16 更新至[30e05a5](https://github.com/satwikkansal/wtfpython/commit/30e05a5973930c38cdb59f1c02b85b19b22ac531)
|
||||||
|
|
||||||
|
* 2022-01-20 开始更新[cd4d7c0](https://github.com/satwikkansal/wtfpython/commit/cd4d7c0e340789bd001e5e9eae0e3c5bb7c7f7f1)
|
122
README.md
122
README.md
@ -30,6 +30,7 @@ PS: 如果你不是第一次读了, 你可以在[这里](https://github.com/satw
|
|||||||
- [Usage/用法](#usage用法)
|
- [Usage/用法](#usage用法)
|
||||||
- [👀 Examples/示例](#-examples示例)
|
- [👀 Examples/示例](#-examples示例)
|
||||||
- [Section: Strain your brain!/大脑运动!](#section-strain-your-brain大脑运动)
|
- [Section: Strain your brain!/大脑运动!](#section-strain-your-brain大脑运动)
|
||||||
|
- [> First things first!/要事优先 *](#-First-things-first!/要事优先-*)
|
||||||
- [> Strings can be tricky sometimes/微妙的字符串 *](#-strings-can-be-tricky-sometimes微妙的字符串-)
|
- [> Strings can be tricky sometimes/微妙的字符串 *](#-strings-can-be-tricky-sometimes微妙的字符串-)
|
||||||
- [> Time for some hash brownies!/是时候来点蛋糕了!](#-time-for-some-hash-brownies是时候来点蛋糕了)
|
- [> Time for some hash brownies!/是时候来点蛋糕了!](#-time-for-some-hash-brownies是时候来点蛋糕了)
|
||||||
- [> Return return everywhere!/到处返回!](#-return-return-everywhere到处返回)
|
- [> Return return everywhere!/到处返回!](#-return-return-everywhere到处返回)
|
||||||
@ -161,6 +162,127 @@ $ pip install wtfpython -U
|
|||||||
|
|
||||||
## Section: Strain your brain!/大脑运动!
|
## Section: Strain your brain!/大脑运动!
|
||||||
|
|
||||||
|
### > First things first!/要事优先 *
|
||||||
|
|
||||||
|
<!-- Example ID: d3d73936-3cf1-4632-b5ab-817981338863 -->
|
||||||
|
<!-- read-only -->
|
||||||
|
|
||||||
|
众所周知,Python 3.8 推出"海象"运算符 (`:=`) 方便易用,让我们一起看看。
|
||||||
|
|
||||||
|
1\.
|
||||||
|
|
||||||
|
```py
|
||||||
|
# Python 版本 3.8+
|
||||||
|
|
||||||
|
>>> a = "wtf_walrus"
|
||||||
|
>>> a
|
||||||
|
'wtf_walrus'
|
||||||
|
|
||||||
|
>>> a := "wtf_walrus"
|
||||||
|
File "<stdin>", line 1
|
||||||
|
a := "wtf_walrus"
|
||||||
|
^
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
>>> (a := "wtf_walrus") # 该语句有效
|
||||||
|
'wtf_walrus'
|
||||||
|
>>> a
|
||||||
|
'wtf_walrus'
|
||||||
|
```
|
||||||
|
|
||||||
|
2 \.
|
||||||
|
|
||||||
|
```py
|
||||||
|
# Python 版本 3.8+
|
||||||
|
|
||||||
|
>>> a = 6, 9
|
||||||
|
>>> a
|
||||||
|
(6, 9)
|
||||||
|
|
||||||
|
>>> (a := 6, 9)
|
||||||
|
(6, 9)
|
||||||
|
>>> a
|
||||||
|
6
|
||||||
|
|
||||||
|
>>> a, b = 6, 9 # 典型拆包操作
|
||||||
|
>>> a, b
|
||||||
|
(6, 9)
|
||||||
|
>>> (a, b = 16, 19) # 出错啦
|
||||||
|
File "<stdin>", line 1
|
||||||
|
(a, b = 16, 19)
|
||||||
|
^
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
>>> (a, b := 16, 19) # 这里的结果是一个奇怪的三元组
|
||||||
|
(6, 16, 19)
|
||||||
|
|
||||||
|
>>> a # a值仍然没变?
|
||||||
|
6
|
||||||
|
|
||||||
|
>>> b
|
||||||
|
16
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 💡 说明
|
||||||
|
|
||||||
|
**“海象”运算符简介**
|
||||||
|
|
||||||
|
海象运算符 (`:=`) 在Python 3.8中被引入,用来在表达式中为变量赋值。
|
||||||
|
|
||||||
|
```py
|
||||||
|
def some_func():
|
||||||
|
# 假设这儿有一些耗时的计算
|
||||||
|
# time.sleep(1000)
|
||||||
|
return 5
|
||||||
|
|
||||||
|
# 引入“海象”运算符前的例子
|
||||||
|
if some_func():
|
||||||
|
print(some_func()) # 糟糕的案例——函数运算了两次
|
||||||
|
|
||||||
|
# 或者,加以改进:
|
||||||
|
a = some_func()
|
||||||
|
if a:
|
||||||
|
print(a)
|
||||||
|
|
||||||
|
# 有了“海象”运算符,你可以写的更简洁:
|
||||||
|
if a := some_func():
|
||||||
|
print(a)
|
||||||
|
```
|
||||||
|
|
||||||
|
**输出 (> Python 3.8):**
|
||||||
|
|
||||||
|
```py
|
||||||
|
5
|
||||||
|
5
|
||||||
|
5
|
||||||
|
```
|
||||||
|
这样既减少了一行代码,又避免了两次调用 `some_func` 函数。
|
||||||
|
|
||||||
|
- 在顶层的无括号赋值操作(使用“海象”运算符)被限制,因此例1中的 `a := "wtf_walrus"` 出现了 `SyntaxError` 。用括号括起来。它就能正常工作了。
|
||||||
|
|
||||||
|
- 一般的,包含 `=` 操作的表达式是不能用括号括起来的,因此 `(a, b = 6, 9)` 中出现了语法错误。
|
||||||
|
|
||||||
|
- “海象”运算符的语法形式为:`NAME:= expr`,`NAME` 是一个有效的标识符,而 `expr` 是一个有效的表达式。 因此,这意味着它不支持可迭代的打包和拆包。
|
||||||
|
|
||||||
|
- `(a := 6, 9)` 等价于 `((a := 6), 9)` ,最终得到 `(a, 9) ` (其中 `a` 的值为6)
|
||||||
|
|
||||||
|
```py
|
||||||
|
>>> (a := 6, 9) == ((a := 6), 9)
|
||||||
|
True
|
||||||
|
>>> x = (a := 696, 9)
|
||||||
|
>>> x
|
||||||
|
(696, 9)
|
||||||
|
>>> x[0] is a # 两个变量指向同一内存空间
|
||||||
|
True
|
||||||
|
```
|
||||||
|
|
||||||
|
- 类似的, `(a, b := 16, 19)` 等价于 `(a, (b := 16), 19)` ,只是一个三元组。
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### > Strings can be tricky sometimes/微妙的字符串 *
|
### > Strings can be tricky sometimes/微妙的字符串 *
|
||||||
|
|
||||||
1\.
|
1\.
|
||||||
|
Loading…
Reference in New Issue
Block a user