Skip to content

Commit 1d2527e

Browse files
committed
新增 10 篇文章
1 parent 8cabc69 commit 1d2527e

20 files changed

+1406
-0
lines changed

source/c01/c01_32.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 1.32 如何让大数变得更易于阅读?
2+
3+
![](http://image.iswbm.com/20200804124133.png)
4+
5+
当一个数非常大时,可能过百万,也可能上亿,太多位的数字 ,会给我们阅读带来很大的障碍。
6+
7+
比如下面这个数,你能一下子说出它是多少万呢,还是多少亿呢?
8+
9+
```
10+
281028344
11+
```
12+
13+
是不是没法很快的辩识出来?
14+
15+
这时候,你可以使用 `_` 来辅助标识,写成这样子就清晰多了
16+
17+
```
18+
281_028_344
19+
```
20+
21+
关键这种写法,在代码中并不会报错噢(Python2 不支持)
22+
23+
```python
24+
>>> number=281_028_344
25+
>>> number
26+
281028344
27+
```
28+
29+
30+
31+
![](http://image.iswbm.com/20200607174235.png)

source/c01/c01_32.rst

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1.32 如何让大数变得更易于阅读?
2+
===============================
3+
4+
|image0|
5+
6+
当一个数非常大时,可能过百万,也可能上亿,太多位的数字
7+
,会给我们阅读带来很大的障碍。
8+
9+
比如下面这个数,你能一下子说出它是多少万呢,还是多少亿呢?
10+
11+
::
12+
13+
281028344
14+
15+
是不是没法很快的辩识出来?
16+
17+
这时候,你可以使用 ``_`` 来辅助标识,写成这样子就清晰多了
18+
19+
::
20+
21+
281_028_344
22+
23+
关键这种写法,在代码中并不会报错噢(Python2 不支持)
24+
25+
.. code:: python
26+
27+
>>> number=281_028_344
28+
>>> number
29+
281028344
30+
31+
|image1|
32+
33+
.. |image0| image:: http://image.iswbm.com/20200804124133.png
34+
.. |image1| image:: http://image.iswbm.com/20200607174235.png
35+

source/c06/c06_07.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 6.7 利用 any 代替 for 循环
2+
3+
![](http://image.iswbm.com/20200804124133.png)
4+
5+
在某些场景下,我们需要判断是否满足某一组集合中任意一个条件
6+
7+
这时候,很多同学自然会想到使用 for 循环。
8+
9+
```python
10+
found = False
11+
for thing in things:
12+
if thing == other_thing:
13+
found = True
14+
break
15+
```
16+
17+
但其实更好的写法,是使用 `any()` 函数,能够使这段代码变得更加清晰、简洁
18+
19+
```python
20+
found = any(thing == other_thing for thing in things)
21+
```
22+
23+
使用 any 并不会减少 for 循环的次数,只要有一个条件为 True,any 就能得到结果。
24+
25+
同理,当你需要判断是否满足某一组集合中所有条件,也可以使用 `all()` 函数。
26+
27+
```python
28+
found = all(thing == other_thing for thing in things)
29+
```
30+
31+
只要有一个不满足条件,all 函数的结果就会立刻返回 False
32+
33+
34+
35+
![](http://image.iswbm.com/20200607174235.png)

source/c06/c06_07.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
6.7 利用 any 代替 for 循环
2+
==========================
3+
4+
|image0|
5+
6+
在某些场景下,我们需要判断是否满足某一组集合中任意一个条件
7+
8+
这时候,很多同学自然会想到使用 for 循环。
9+
10+
.. code:: python
11+
12+
found = False
13+
for thing in things:
14+
if thing == other_thing:
15+
found = True
16+
break
17+
18+
但其实更好的写法,是使用 ``any()``
19+
函数,能够使这段代码变得更加清晰、简洁
20+
21+
.. code:: python
22+
23+
found = any(thing == other_thing for thing in things)
24+
25+
使用 any 并不会减少 for 循环的次数,只要有一个条件为 True,any
26+
就能得到结果。
27+
28+
同理,当你需要判断是否满足某一组集合中所有条件,也可以使用 ``all()``
29+
函数。
30+
31+
.. code:: python
32+
33+
found = all(thing == other_thing for thing in things)
34+
35+
只要有一个不满足条件,all 函数的结果就会立刻返回 False
36+
37+
|image1|
38+
39+
.. |image0| image:: http://image.iswbm.com/20200804124133.png
40+
.. |image1| image:: http://image.iswbm.com/20200607174235.png
41+

source/c06/c06_08.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 6.8 不同条件分支里应减少重合度
2+
3+
![](http://image.iswbm.com/20200804124133.png)
4+
5+
如下是一个简单的条件语句模型
6+
7+
```python
8+
if A:
9+
<code_block_1>
10+
elif B:
11+
<code_block_2>
12+
else:
13+
<code_block_3>
14+
```
15+
16+
如果 code_block_2 和 code_block_1 的代码完全一致,那么应该想办法减少代码冗余。
17+
18+
这边举个例子,下面这段代码中
19+
20+
```python
21+
def process_payment(payment):
22+
if payment.currency == 'USD':
23+
process_standard_payment(payment)
24+
elif payment.currency == 'EUR':
25+
process_standard_payment(payment)
26+
else:
27+
process_international_payment(payment)
28+
```
29+
30+
其实更好的做法是用 in 来合并条件一和条件二
31+
32+
```python
33+
def process_payment(payment):
34+
if payment.currency in ('USD', 'EUR'):
35+
process_standard_payment(payment)
36+
else:
37+
process_international_payment(payment)
38+
```
39+
40+
41+
42+
![](http://image.iswbm.com/20200607174235.png)

source/c06/c06_08.rst

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
6.8 不同条件分支里应减少重合度
2+
==============================
3+
4+
|image0|
5+
6+
如下是一个简单的条件语句模型
7+
8+
.. code:: python
9+
10+
if A:
11+
<code_block_1>
12+
elif B:
13+
<code_block_2>
14+
else:
15+
<code_block_3>
16+
17+
如果 code_block_2 和 code_block_1
18+
的代码完全一致,那么应该想办法减少代码冗余。
19+
20+
这边举个例子,下面这段代码中
21+
22+
.. code:: python
23+
24+
def process_payment(payment):
25+
if payment.currency == 'USD':
26+
process_standard_payment(payment)
27+
elif payment.currency == 'EUR':
28+
process_standard_payment(payment)
29+
else:
30+
process_international_payment(payment)
31+
32+
其实更好的做法是用 in 来合并条件一和条件二
33+
34+
.. code:: python
35+
36+
def process_payment(payment):
37+
if payment.currency in ('USD', 'EUR'):
38+
process_standard_payment(payment)
39+
else:
40+
process_international_payment(payment)
41+
42+
|image1|
43+
44+
.. |image0| image:: http://image.iswbm.com/20200804124133.png
45+
.. |image1| image:: http://image.iswbm.com/20200607174235.png
46+

source/c06/c06_09.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 6.9 如无必要,勿增实体噢
2+
3+
![](http://image.iswbm.com/20200804124133.png)
4+
5+
## 删除没必要的调用`keys()`
6+
7+
字典是由一个个的键值对组成的,如果你遍历字典时只需要访问键,用不到值,有很多同学会用下面这种方式:
8+
9+
```python
10+
for currency in currencies.keys():
11+
process(currency)
12+
```
13+
14+
在这种情况下,不需要调用keys(),因为遍历字典时的默认行为是遍历键。
15+
16+
```python
17+
for currency in currencies:
18+
process(currency)
19+
```
20+
21+
现在,该代码更加简洁,易于阅读,并且避免调用函数会带来性能改进。
22+
23+
## 简化序列比较
24+
25+
我们经常要做的是在尝试对列表或序列进行操作之前检查列表或序列是否包含元素。
26+
27+
```python
28+
if len(list_of_hats) > 0:
29+
hat_to_wear = choose_hat(list_of_hats)
30+
```
31+
32+
使用Python的方法则更加简单:如果Python列表和序列具有元素,则返回为True,否则为False:
33+
34+
```python
35+
if list_of_hats:
36+
hat_to_wear = choose_hat(list_of_hats)
37+
```
38+
39+
## 仅使用一次的内联变量
40+
41+
我们在很多代码中经常看到,有些同学分配结果给变量,然后马上返回它,例如,
42+
43+
```python
44+
def state_attributes(self):
45+
"""Return the state attributes."""
46+
state_attr = {
47+
ATTR_CODE_FORMAT: self.code_format,
48+
ATTR_CHANGED_BY: self.changed_by,
49+
}
50+
return state_attr
51+
```
52+
53+
如果直接返回,则更加直观、简洁,
54+
55+
```python
56+
def state_attributes(self):
57+
"""Return the state attributes."""
58+
return {
59+
ATTR_CODE_FORMAT: self.code_format,
60+
ATTR_CHANGED_BY: self.changed_by,
61+
}
62+
```
63+
64+
这样可以缩短代码并删除不必要的变量,从而减轻了读取函数的负担。
65+
66+
67+
68+
![](http://image.iswbm.com/20200607174235.png)

source/c06/c06_09.rst

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
6.9 如无必要,勿增实体噢
2+
========================
3+
4+
|image0|
5+
6+
删除没必要的调用\ ``keys()``
7+
----------------------------
8+
9+
字典是由一个个的键值对组成的,如果你遍历字典时只需要访问键,用不到值,有很多同学会用下面这种方式:
10+
11+
.. code:: python
12+
13+
for currency in currencies.keys():
14+
process(currency)
15+
16+
在这种情况下,不需要调用keys(),因为遍历字典时的默认行为是遍历键。
17+
18+
.. code:: python
19+
20+
for currency in currencies:
21+
process(currency)
22+
23+
现在,该代码更加简洁,易于阅读,并且避免调用函数会带来性能改进。
24+
25+
简化序列比较
26+
------------
27+
28+
我们经常要做的是在尝试对列表或序列进行操作之前检查列表或序列是否包含元素。
29+
30+
.. code:: python
31+
32+
if len(list_of_hats) > 0:
33+
hat_to_wear = choose_hat(list_of_hats)
34+
35+
使用Python的方法则更加简单:如果Python列表和序列具有元素,则返回为True,否则为False:
36+
37+
.. code:: python
38+
39+
if list_of_hats:
40+
hat_to_wear = choose_hat(list_of_hats)
41+
42+
仅使用一次的内联变量
43+
--------------------
44+
45+
我们在很多代码中经常看到,有些同学分配结果给变量,然后马上返回它,例如,
46+
47+
.. code:: python
48+
49+
def state_attributes(self):
50+
"""Return the state attributes."""
51+
state_attr = {
52+
ATTR_CODE_FORMAT: self.code_format,
53+
ATTR_CHANGED_BY: self.changed_by,
54+
}
55+
return state_attr
56+
57+
如果直接返回,则更加直观、简洁,
58+
59+
.. code:: python
60+
61+
def state_attributes(self):
62+
"""Return the state attributes."""
63+
return {
64+
ATTR_CODE_FORMAT: self.code_format,
65+
ATTR_CHANGED_BY: self.changed_by,
66+
}
67+
68+
这样可以缩短代码并删除不必要的变量,从而减轻了读取函数的负担。
69+
70+
|image1|
71+
72+
.. |image0| image:: http://image.iswbm.com/20200804124133.png
73+
.. |image1| image:: http://image.iswbm.com/20200607174235.png
74+

0 commit comments

Comments
 (0)