代码如诗

Python 代码风格规范 (PEP8)
### 一般注释 ```python some_code() more_code_here() # One sentence explaining if not data: do_something_here() other_code() ```
### 行内注释 ```python words = 'Hello, World!' # Some comment here something = 'Hahaha' # 注释不需要对齐 ```
#### 块注释 ```python # 通常在需要 review 的晦涩难懂的代码前 # 留下 TODO、待 review 的说明 # 不要在这里流水帐式描述你的代码 # 如: 「此处判定列表各项均为有效值,然后我们...」 # 要假定 reviewer 比你更懂 Python if 0 in [!int(node) for node in li]: # 恰到好处的注释 ``` > 如果代码不够清晰以至于需要一个注释,那么或许它应该被重写。
#### 函数注释 ```python def print(arg): """将 arg 打印在屏幕上 这个函数会调用内置方法讲 arg 输出到屏幕上, 注意这个函数不是线程安全的。 参数: arg: 字符串或可以调用 arg.__str__() 的对象 返回值: 返回一个整数,代表输出的字符串长度 抛出异常: NameError: 参数没有 __str__() 方法供转化成字符串。 """ do_some_thing() ```
#### 类注释 ```python class SampleClass: """简单描述这个类 详细说明这个类... 可以有很多行说明... 类成员说名: Blablabla """ def __init__(self, *args): """类方法说明""" ```

String

需要拼接有空格的字符串,运用占位符 ```python x = '%s, %s!' % (a, b) x = '{}, {}!'.format(a, b) x = ', '.join([a, b]) ``` 直接拼接的字符串 ```python x = a + b ```
> 注意:使用加法循环拼接字符串,可能导致二次而非线性的运行时间。 BAD ```python li = ['a', 'very', 'long', 'list'] result = '' for i in li: result = result + i print(result) ``` GOOD ```python result = ''.join(li) print(result) ```
当字符串过长时,使用 `\` 漂亮地分割字符串 ```python s = '这是一个' \ '很长的字符串' ```
其他乱七八糟但很重要的东西

类名以写字母开头 ClassName

驼峰名法 someFunctionName

下划线_命名法 some_variable_name

### 遍历同时操作数组 > 不规范的行为 ```python li = list(range(10)) for i in li: li.append(i) ```
### 传参传字典或列表 ```python def foo(i, arg=[]): # 正确做法 arg=None arg.append(i) print(arg) foo('a') # Output: ['a'] foo('b') # Output: ['a', 'b'] ```
### 文件中读入的字符莫名消失 复现代码 ```python raw = '第一行\n\r第二行' for i in raw.split('\n'): print('---start', i, 'end---') # Output: # ---start 第一行 end--- # 第二行 end--- ```
GOOD ```python if age < 18: return 'You are too small' do_something_here() ``` BAD ```python if age > 18: do_something_here() else: return 'You are too small' ```
#### 善用 if GOOD ```python if data: break ``` BAD ```python if data != []: break ```
## 忍者代码
#### 没人知道这个变量是什么 - a, b, j, k, l - str, string, number, list, dict - data - data1 - data114514 以及见鬼的缩写 - `ObjStuAre_id`: object_student_area_id
> 一个变量,一杯茶,一个bug修一天 - `data` - `date` - `l` - `I`
#### 没人知道这两是不是同一个东西 - `showMessage` - `displayMessage` - `printMessage`
#### 夸张的变量名 - `very_super_powerful_print_function` - `lovely_variable` - `nice_list`
#### 重叠外部命名空间变量 ```python id = 39 def foo(): id += 1 # 一堆代码 print(id) ```
#### Powerful Function ! 例如某个 `check_empty_in_list()`,按字面意思会检查列表中是否有空项,可这个函数在检查到空项的同时还会「智能地」帮你删掉空项。然而使用函数的人对此一无所知,函数作者也忘了有这么回事。

一行!展现你的聪明才智!

result = not os.system(' '.join(['proxychains', '-q', 'pandoc', 'README.md', 'SUMMARY.md', ' '.join([str(i)+'.md' for i in range(3, 109+1)]), '-o', '/tmp/out.epub']) or print("Failed")
#### References - [现代 JavaScript 教程 - 忍者代码](https://zh.javascript.infoi/ninja-code) - [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html) - [水族馆](https://aquarium39.moe) - [你所不知道的Python冷知識](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/649905/)