知行编程网知行编程网  2022-07-23 08:00 知行编程网 隐藏边栏 |   抢沙发  0 
文章评分 0 次,平均分 0.0

来源丨https://github.com/jackzhenguo/python-small-examples


一、 数字

1 求绝对值

绝对值或复数的模

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">abs</span>(<span style="color: rgb(0, 92, 197);">-6</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">6</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

2 进制转化

十进制转换为二进制:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">bin</span>(<span style="color: rgb(0, 92, 197);">10</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(3, 47, 98);">'0b1010'</span>

十进制转换为八进制:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">oct</span>(<span style="color: rgb(0, 92, 197);">9</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(3, 47, 98);">'0o11'</span>

十进制转换为十六进制:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">hex</span>(<span style="color: rgb(0, 92, 197);">15</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(3, 47, 98);">'0xf'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

3 整数和ASCII互转

十进制整数对应的ASCII字符

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">chr</span>(<span style="color: rgb(0, 92, 197);">65</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(3, 47, 98);">'A'</span>

查看某个ASCII字符对应的十进制数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">ord</span>(<span style="color: rgb(3, 47, 98);">'A'</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">65</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

4 元素都为真检查

所有元素都为真,返回 True,否则为False

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">5</span>]: <span style="color: rgb(111, 66, 193);">all</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">6</span>])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">5</span>]: <span style="color: rgb(0, 92, 197);">False</span>
<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">6</span>]: <span style="color: rgb(111, 66, 193);">all</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>])<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">6</span>]: <span style="color: rgb(0, 92, 197);">True</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

5 元素至少一个为真检查 

至少有一个元素为真返回True,否则False

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">7</span>]: <span style="color: rgb(111, 66, 193);">any</span>([<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">0</span>,[]])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">7</span>]: <span style="color: rgb(0, 92, 197);">False</span>
<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">8</span>]: <span style="color: rgb(111, 66, 193);">any</span>([<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">1</span>])<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">8</span>]: <span style="color: rgb(0, 92, 197);">True</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

6 判断是真是假  

测试一个对象是True, 还是False.

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">9</span>]: <span style="color: rgb(111, 66, 193);">bool</span>([<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">0</span>])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">9</span>]: <span style="color: rgb(0, 92, 197);">True</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">10</span>]: <span style="color: rgb(111, 66, 193);">bool</span>([])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">10</span>]: <span style="color: rgb(0, 92, 197);">False</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">11</span>]: <span style="color: rgb(111, 66, 193);">bool</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">1</span>])<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">11</span>]: <span style="color: rgb(0, 92, 197);">True</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

7 创建复数

创建一个复数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">complex</span>(<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">2</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: (<span style="color: rgb(0, 92, 197);">1+2j</span>)</p><p><br  /></p>

8 取商和余数  

分别取商和余数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">divmod</span>(<span style="color: rgb(0, 92, 197);">10</span>,<span style="color: rgb(0, 92, 197);">3</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: (<span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">1</span>)</p><p><br  /></p>

9 转为浮点类型 

将一个整数或数值型字符串转换为浮点数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">float</span>(<span style="color: rgb(0, 92, 197);">3</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">3.0</span>

如果不能转化为浮点数,则会报ValueError:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">float</span>(<span style="color: rgb(3, 47, 98);">'a'</span>)<br  /><p><span style="color: rgb(106, 115, 125);"># ValueError: could not convert string to float: 'a'</span></p><p><span style="color: rgb(106, 115, 125);"><br  /></span></p>

10 转为整型  

int(x, base =10) , x可能为字符串或数值,将x 转换为一个普通整数。如果参数是字符串,那么它可能包含符号和小数点。如果超出了普通整数的表示范围,一个长整数被返回。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">int</span>(<span style="color: rgb(3, 47, 98);">'12'</span>,<span style="color: rgb(0, 92, 197);">16</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">18</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

11 次幂

base为底的exp次幂,如果mod给出,取余

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">pow</span>(<span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">4</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">1</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

12 四舍五入

四舍五入,ndigits代表小数点后保留几位:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">11</span>]: <span style="color: rgb(111, 66, 193);">round</span>(<span style="color: rgb(0, 92, 197);">10.0222222</span>, <span style="color: rgb(0, 92, 197);">3</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">11</span>]: <span style="color: rgb(0, 92, 197);">10.022</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">12</span>]: <span style="color: rgb(111, 66, 193);">round</span>(<span style="color: rgb(0, 92, 197);">10.05</span>,<span style="color: rgb(0, 92, 197);">1</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">12</span>]: <span style="color: rgb(0, 92, 197);">10.1</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

13 链式比较

i <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">3</span><br  /><span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(0, 92, 197);">1</span> <span style="color: rgb(0, 92, 197);"><</span> i <span style="color: rgb(0, 92, 197);"><</span> <span style="color: rgb(0, 92, 197);">3</span>)  <span style="color: rgb(106, 115, 125);"># False</span><br  /><p><span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(0, 92, 197);">1</span> <span style="color: rgb(0, 92, 197);"><</span> i <span style="color: rgb(0, 92, 197);"><=</span> <span style="color: rgb(0, 92, 197);">3</span>)  <span style="color: rgb(106, 115, 125);"># True</span></p><p><span style="color: rgb(106, 115, 125);"><br  /></span></p>

二、 字符串

14 字符串转字节  

字符串转换为字节类型

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">12</span>]: s <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">"apple"</span>                                                            <br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">13</span>]: <span style="color: rgb(111, 66, 193);">bytes</span>(s,encoding<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'utf-8'</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">13</span>]: <span style="color: rgb(3, 47, 98);">b'apple'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

15 任意对象转为字符串  

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">14</span>]: i <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">100</span>                                                                <br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">15</span>]: <span style="color: rgb(111, 66, 193);">str</span>(i)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">15</span>]: <span style="color: rgb(3, 47, 98);">'100'</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">16</span>]: <span style="color: rgb(111, 66, 193);">str</span>([])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">16</span>]: <span style="color: rgb(3, 47, 98);">'[]'</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">17</span>]: <span style="color: rgb(111, 66, 193);">str</span>(<span style="color: rgb(111, 66, 193);">tuple</span>())<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">17</span>]: <span style="color: rgb(3, 47, 98);">'()'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

16 执行字符串表示的代码

将字符串编译成python能识别或可执行的代码,也可以将文字读成字符串再编译。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: s  <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">"print('helloworld')"</span><br  />    <br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: r <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">compile</span>(s,<span style="color: rgb(3, 47, 98);">"<string>"</span>, <span style="color: rgb(3, 47, 98);">"exec"</span>)<br  />    <br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: r<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);"><</span>code object <span style="color: rgb(0, 92, 197);"><</span>module<span style="color: rgb(0, 92, 197);">></span> at <span style="color: rgb(0, 92, 197);">0x0000000005DE75D0</span>, file <span style="color: rgb(3, 47, 98);">"<string>"</span>, line <span style="color: rgb(0, 92, 197);">1></span><br  />    <br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">exec</span>(r)<br  /><p>helloworld</p><p><br  /></p>

17 计算表达式

将字符串str 当成有效的表达式来求值并返回计算结果取出字符串中内容

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: s <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">"1 + 3 +5"</span><br  />    ...: <span style="color: rgb(111, 66, 193);">eval</span>(s)<br  />    ...:<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">9</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

18 字符串格式化 

格式化输出字符串,format(value, format_spec)实质上是调用了value的__format__(format_spec)方法。

3.1415926 {:.2f} 3.14 保留小数点后两位
3.1415926 {:+.2f} +3.14 带符号保留小数点后两位
-1 {:+.2f} -1.00 带符号保留小数点后两位
2.71828 {:.0f} 3 不带小数
5 {:0>2d} 05 数字补零 (填充左边, 宽度为2)
5 {:x<4d} 5xxx 数字补x (填充右边, 宽度为4)
10 {:x<4d} 10xx 数字补x (填充右边, 宽度为4)
1000000 {:,} 1,000,000 以逗号分隔的数字格式
0.25 {:.2%} 25.00% 百分比格式
1000000000 {:.2e} 1.00e+09 指数记法
18 {:>10d} ' 18' 右对齐 (默认, 宽度为10)
18 {:<10d} '18 ' 左对齐 (宽度为10)
18 {:^10d} ' 18 ' 中间对齐 (宽度为10)


三、 函数

19 拿来就用的排序函数

排序:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: a <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">1</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">sorted</span>(a,reverse<span style="color: rgb(0, 92, 197);">=True</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: [<span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">1</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: a <span style="color: rgb(0, 92, 197);">=</span> [{<span style="color: rgb(3, 47, 98);">'name'</span>:<span style="color: rgb(3, 47, 98);">'xiaoming'</span>,<span style="color: rgb(3, 47, 98);">'age'</span>:<span style="color: rgb(0, 92, 197);">18</span>,<span style="color: rgb(3, 47, 98);">'gender'</span>:<span style="color: rgb(3, 47, 98);">'male'</span>},{<span style="color: rgb(3, 47, 98);">'name'</span>:<span style="color: rgb(3, 47, 98);">'</span><br  /><span style="color: rgb(3, 47, 98);">     ...: xiaohong'</span>,<span style="color: rgb(3, 47, 98);">'age'</span>:<span style="color: rgb(0, 92, 197);">20</span>,<span style="color: rgb(3, 47, 98);">'gender'</span>:<span style="color: rgb(3, 47, 98);">'female'</span>}]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">sorted</span>(a,key<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(215, 58, 73);">lambda</span> x: x[<span style="color: rgb(3, 47, 98);">'age'</span>],reverse<span style="color: rgb(0, 92, 197);">=False</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]:<br  />[{<span style="color: rgb(3, 47, 98);">'name'</span>: <span style="color: rgb(3, 47, 98);">'xiaoming'</span>, <span style="color: rgb(3, 47, 98);">'age'</span>: <span style="color: rgb(0, 92, 197);">18</span>, <span style="color: rgb(3, 47, 98);">'gender'</span>: <span style="color: rgb(3, 47, 98);">'male'</span>},<br  /><p>{<span style="color: rgb(3, 47, 98);">'name'</span>: <span style="color: rgb(3, 47, 98);">'xiaohong'</span>, <span style="color: rgb(3, 47, 98);">'age'</span>: <span style="color: rgb(0, 92, 197);">20</span>, <span style="color: rgb(3, 47, 98);">'gender'</span>: <span style="color: rgb(3, 47, 98);">'female'</span>}]</p><p><br  /></p>

20 求和函数

求和:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">181</span>]: a <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">1</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">182</span>]: <span style="color: rgb(111, 66, 193);">sum</span>(a)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">182</span>]: <span style="color: rgb(0, 92, 197);">11</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">185</span>]: <span style="color: rgb(111, 66, 193);">sum</span>(a,<span style="color: rgb(0, 92, 197);">10</span>) <span style="color: rgb(106, 115, 125);">#求和的初始值为10</span><br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">185</span>]: <span style="color: rgb(0, 92, 197);">21</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

21 nonlocal用于内嵌函数中

关键词nonlocal常用于函数嵌套中,声明变量i为非局部变量;如果不声明,i+=1表明i为函数wrapper内的局部变量,因为在i+=1引用(reference)时,i未被声明,所以会报unreferenced variable的错误。

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">excepter</span>(f):<br  />    i <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">0</span><br  />    t1 <span style="color: rgb(0, 92, 197);">=</span> time.<span style="color: rgb(111, 66, 193);">time</span>()<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">wrapper</span>():<br  />        <span style="color: rgb(215, 58, 73);">try</span>:<br  />            <span style="color: rgb(111, 66, 193);">f</span>()<br  />        <span style="color: rgb(215, 58, 73);">except</span> <span style="color: rgb(227, 98, 9);">Exception</span> <span style="color: rgb(215, 58, 73);">as</span> e:<br  />            <span style="color: rgb(215, 58, 73);">nonlocal</span> i<br  />            i <span style="color: rgb(0, 92, 197);">+=</span> <span style="color: rgb(0, 92, 197);">1</span><br  />            <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'<span style="color: rgb(36, 41, 46);">{e.args[<span style="color: rgb(0, 92, 197);">0</span>]}</span>: <span style="color: rgb(36, 41, 46);">{i}</span>'</span>)<br  />            t2 <span style="color: rgb(0, 92, 197);">=</span> time.<span style="color: rgb(111, 66, 193);">time</span>()<br  />            <span style="color: rgb(215, 58, 73);">if</span> i <span style="color: rgb(0, 92, 197);">==</span> n:<br  />                <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'spending time:<span style="color: rgb(36, 41, 46);">{<span style="color: rgb(111, 66, 193);">round</span>(t2<span style="color: rgb(0, 92, 197);">-</span>t1,<span style="color: rgb(0, 92, 197);">2</span>)}</span>'</span>)<br  /><p><span style="color: rgb(215, 58, 73);">return</span> wrapper</p><p><br  /></p>

22 global 声明全局变量

先回答为什么要有global,一个变量被多个函数引用,想让全局变量被所有函数共享。有的伙伴可能会想这还不简单,这样写:

i <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">5</span><br  /><span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">f</span>():<br  />    <span style="color: rgb(111, 66, 193);">print</span>(i)<br  /><br  /><span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">g</span>():<br  />    <span style="color: rgb(111, 66, 193);">print</span>(i)<br  />    <span style="color: rgb(215, 58, 73);">pass</span><br  /><br  /><span style="color: rgb(111, 66, 193);">f</span>()<br  /><span style="color: rgb(111, 66, 193);">g</span>()

f和g两个函数都能共享变量i,程序没有报错,所以他们依然不明白为什么要用global.

但是,如果我想要有个函数对i递增,这样:

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">h</span>():<br  />    i <span style="color: rgb(0, 92, 197);">+=</span> <span style="color: rgb(0, 92, 197);">1</span><br  /><br  /><span style="color: rgb(111, 66, 193);">h</span>()

此时执行程序,bang, 出错了!抛出异常:UnboundLocalError,原来编译器在解释i+=1时会把i解析为函数h()内的局部变量,很显然在此函数内,编译器找不到对变量i的定义,所以会报错。

global就是为解决此问题而被提出,在函数h内,显式地告诉编译器i为全局变量,然后编译器会在函数外面寻找i的定义,执行完i+=1后,i还为全局变量,值加1:

i <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">0</span><br  /><span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">h</span>():<br  />    <span style="color: rgb(215, 58, 73);">global</span> i<br  />    i <span style="color: rgb(0, 92, 197);">+=</span> <span style="color: rgb(0, 92, 197);">1</span><br  /><br  /><span style="color: rgb(111, 66, 193);">h</span>()<br  /><p><span style="color: rgb(111, 66, 193);">print</span>(i)</p><p><br  /></p>

23 交换两元素

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">swap</span>(a, b):<br  />    <span style="color: rgb(215, 58, 73);">return</span> b, a<br  /><br  /><br  /><p><span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(111, 66, 193);">swap</span>(<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">0</span>))  <span style="color: rgb(106, 115, 125);"># (0,1)</span></p><p><span style="color: rgb(106, 115, 125);"><br  /></span></p>

24 操作函数对象

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">31</span>]: def <span style="color: rgb(111, 66, 193);">f</span>():<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">'i'm f'</span>)<br  />    ...:<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">32</span>]: <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">g</span>():<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">'i'm g'</span>)<br  />    ...:<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">33</span>]: [f,g][<span style="color: rgb(0, 92, 197);">1</span>]()<br  />i'm g

创建函数对象的list,根据想要调用的index,方便统一调用。


25 生成逆序序列

<span style="color: rgb(111, 66, 193);">list</span>(<span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">10</span>,<span style="color: rgb(0, 92, 197);">-1</span>,<span style="color: rgb(0, 92, 197);">-1</span>)) <span style="color: rgb(106, 115, 125);"># [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]</span>

第三个参数为负时,表示从第一个参数开始递减,终止到第二个参数(不包括此边界)


26 函数的五类参数使用例子

python五类参数:位置参数,关键字参数,默认参数,可变位置或关键字参数的使用。

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">f</span>(a,<span style="color: rgb(0, 92, 197);">*</span>b,c<span style="color: rgb(0, 92, 197);">=10</span>,<span style="color: rgb(0, 92, 197);">**</span>d):<br  />  <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'a:<span style="color: rgb(36, 41, 46);">{a}</span>,b:<span style="color: rgb(36, 41, 46);">{b}</span>,c:<span style="color: rgb(36, 41, 46);">{c}</span>,d:<span style="color: rgb(36, 41, 46);">{d}</span>'</span>)

默认参数c不能位于可变关键字参数d后.

调用f:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">10</span>]: <span style="color: rgb(111, 66, 193);">f</span>(<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">5</span>,width<span style="color: rgb(0, 92, 197);">=10</span>,height<span style="color: rgb(0, 92, 197);">=20</span>)<br  />a:<span style="color: rgb(0, 92, 197);">1</span>,b:(<span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">5</span>),c:<span style="color: rgb(0, 92, 197);">10</span>,d:{<span style="color: rgb(3, 47, 98);">'width'</span>: <span style="color: rgb(0, 92, 197);">10</span>, <span style="color: rgb(3, 47, 98);">'height'</span>: <span style="color: rgb(0, 92, 197);">20</span>}

可变位置参数b实参后被解析为元组(2,5);而c取得默认值10; d被解析为字典.

再次调用f:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">11</span>]: <span style="color: rgb(111, 66, 193);">f</span>(a<span style="color: rgb(0, 92, 197);">=1</span>,c<span style="color: rgb(0, 92, 197);">=12</span>)<br  />a:<span style="color: rgb(0, 92, 197);">1</span>,b:(),c:<span style="color: rgb(0, 92, 197);">12</span>,d:{}

a=1传入时a就是关键字参数,b,d都未传值,c被传入12,而非默认值。

注意观察参数a, 既可以f(1),也可以f(a=1) 其可读性比第一种更好,建议使用f(a=1)。如果要强制使用f(a=1),需要在前面添加一个星号:


<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">f</span>(*,a,<span style="color: rgb(0, 92, 197);">**</span>b):<br  />  <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'a:<span style="color: rgb(36, 41, 46);">{a}</span>,b:<span style="color: rgb(36, 41, 46);">{b}</span>'</span>)

此时f(1)调用,将会报错:TypeError: f() takes 0 positional arguments but 1 was given

只能f(a=1)才能OK.

说明前面的*发挥作用,它变为只能传入关键字参数,那么如何查看这个参数的类型呢?借助python的inspect模块:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">22</span>]: for name,val <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">signature</span>(f).parameters.<span style="color: rgb(111, 66, 193);">items</span>():<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(name,val.kind)<br  />    ...:<br  />a <span style="color: rgb(227, 98, 9);">KEYWORD_ONLY</span><br  />b <span style="color: rgb(227, 98, 9);">VAR_KEYWORD</span>

可看到参数a的类型为KEYWORD_ONLY,也就是仅仅为关键字参数。

但是,如果f定义为:

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">f</span>(a,<span style="color: rgb(0, 92, 197);">*</span>b):<br  />  <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'a:<span style="color: rgb(36, 41, 46);">{a}</span>,b:<span style="color: rgb(36, 41, 46);">{b}</span>'</span>)

查看参数类型:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">24</span>]: for name,val <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">signature</span>(f).parameters.<span style="color: rgb(111, 66, 193);">items</span>():<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(name,val.kind)<br  />    ...:<br  />a <span style="color: rgb(227, 98, 9);">POSITIONAL_OR_KEYWORD</span><br  />b <span style="color: rgb(227, 98, 9);">VAR_POSITIONAL</span>

可以看到参数a既可以是位置参数也可是关键字参数。


27 使用slice对象

生成关于蛋糕的序列cake1:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: cake1 <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">list</span>(<span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">-1</span>))<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: b <span style="color: rgb(0, 92, 197);">=</span> cake1[<span style="color: rgb(0, 92, 197);">1</span>:<span style="color: rgb(0, 92, 197);">10</span>:<span style="color: rgb(0, 92, 197);">2</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: b<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: [<span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">2</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: cake1<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: [<span style="color: rgb(0, 92, 197);">5</span>, <span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">1</span>]

再生成一个序列:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">5</span>]: from random <span style="color: rgb(215, 58, 73);">import</span> randint<br  />   ...: cake2 <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(111, 66, 193);">randint</span>(<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">100</span>) <span style="color: rgb(215, 58, 73);">for</span> _ <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">100</span>)]<br  />   ...: <span style="color: rgb(106, 115, 125);"># 同样以间隔为2切前10个元素,得到切片d</span><br  />   ...: d <span style="color: rgb(0, 92, 197);">=</span> cake2[<span style="color: rgb(0, 92, 197);">1</span>:<span style="color: rgb(0, 92, 197);">10</span>:<span style="color: rgb(0, 92, 197);">2</span>]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">6</span>]: d<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">6</span>]: [<span style="color: rgb(0, 92, 197);">75</span>, <span style="color: rgb(0, 92, 197);">33</span>, <span style="color: rgb(0, 92, 197);">63</span>, <span style="color: rgb(0, 92, 197);">93</span>, <span style="color: rgb(0, 92, 197);">15</span>]

你看,我们使用同一种切法,分别切开两个蛋糕cake1,cake2. 后来发现这种切法极为经典,又拿它去切更多的容器对象。

那么,为什么不把这种切法封装为一个对象呢?于是就有了slice对象。

定义slice对象极为简单,如把上面的切法定义成slice对象:

perfect_cake_slice_way <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">slice</span>(<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">10</span>,<span style="color: rgb(0, 92, 197);">2</span>)<br  /><span style="color: rgb(106, 115, 125);">#去切cake1</span><br  />cake1_slice <span style="color: rgb(0, 92, 197);">=</span> cake1[perfect_cake_slice_way]<br  />cake2_slice <span style="color: rgb(0, 92, 197);">=</span> cake2[perfect_cake_slice_way]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">11</span>]: cake1_slice<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">11</span>]: [<span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">2</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">12</span>]: cake2_slice<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">12</span>]: [<span style="color: rgb(0, 92, 197);">75</span>, <span style="color: rgb(0, 92, 197);">33</span>, <span style="color: rgb(0, 92, 197);">63</span>, <span style="color: rgb(0, 92, 197);">93</span>, <span style="color: rgb(0, 92, 197);">15</span>]

与上面的结果一致。

对于逆向序列切片,slice对象一样可行:

a <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">7</span>,<span style="color: rgb(0, 92, 197);">9</span>,<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">7</span>]<br  />a_ <span style="color: rgb(0, 92, 197);">=</span> a[<span style="color: rgb(0, 92, 197);">5</span>:<span style="color: rgb(0, 92, 197);">1</span>:<span style="color: rgb(0, 92, 197);">-1</span>]<br  /><br  />named_slice <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">slice</span>(<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">-1</span>)<br  />a_slice <span style="color: rgb(0, 92, 197);">=</span> a[named_slice]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">14</span>]: a_<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">14</span>]: [<span style="color: rgb(0, 92, 197);">0</span>, <span style="color: rgb(0, 92, 197);">9</span>, <span style="color: rgb(0, 92, 197);">7</span>, <span style="color: rgb(0, 92, 197);">5</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">15</span>]: a_slice<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">15</span>]: [<span style="color: rgb(0, 92, 197);">0</span>, <span style="color: rgb(0, 92, 197);">9</span>, <span style="color: rgb(0, 92, 197);">7</span>, <span style="color: rgb(0, 92, 197);">5</span>]

频繁使用同一切片的操作可使用slice对象抽出来,复用的同时还能提高代码可读性。


28 lambda 函数的动画演示

有些读者反映,lambda函数不太会用,问我能不能解释一下。

比如,下面求这个 lambda函数:

<span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">max_len</span>(<span style="color: rgb(0, 92, 197);">*</span>lists):<br  />    <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(111, 66, 193);">max</span>(<span style="color: rgb(0, 92, 197);">*</span>lists, key<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(215, 58, 73);">lambda</span> v: <span style="color: rgb(111, 66, 193);">len</span>(v))

有两点疑惑:

  • 参数v的取值?

  • lambda函数有返回值吗?如果有,返回值是多少?

调用上面函数,求出以下三个最长的列表:

r <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">max_len</span>([<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">3</span>], [<span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">5</span>, <span style="color: rgb(0, 92, 197);">6</span>, <span style="color: rgb(0, 92, 197);">7</span>], [<span style="color: rgb(0, 92, 197);">8</span>])<br  /><span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'更长的列表是<span style="color: rgb(36, 41, 46);">{r}</span>'</span>)

程序完整运行过程,动画演示如下:


结论:

  • 参数v的可能取值为*lists,也就是 tuple 的一个元素。

  • lambda函数返回值,等于lambda v冒号后表达式的返回值。

四、 数据结构

29 转为字典  

创建数据字典

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">dict</span>()<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: {}<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">dict</span>(a<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'a'</span>,b<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'b'</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: {<span style="color: rgb(3, 47, 98);">'a'</span>: <span style="color: rgb(3, 47, 98);">'a'</span>, <span style="color: rgb(3, 47, 98);">'b'</span>: <span style="color: rgb(3, 47, 98);">'b'</span>}<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">dict</span>(<span style="color: rgb(111, 66, 193);">zip</span>([<span style="color: rgb(3, 47, 98);">'a'</span>,<span style="color: rgb(3, 47, 98);">'b'</span>],[<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">2</span>]))<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: {<span style="color: rgb(3, 47, 98);">'a'</span>: <span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(3, 47, 98);">'b'</span>: <span style="color: rgb(0, 92, 197);">2</span>}<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">dict</span>([(<span style="color: rgb(3, 47, 98);">'a'</span>,<span style="color: rgb(0, 92, 197);">1</span>),(<span style="color: rgb(3, 47, 98);">'b'</span>,<span style="color: rgb(0, 92, 197);">2</span>)])<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: {<span style="color: rgb(3, 47, 98);">'a'</span>: <span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(3, 47, 98);">'b'</span>: <span style="color: rgb(0, 92, 197);">2</span>}</p><p><br  /></p>

30 冻结集合  

创建一个不可修改的集合。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">frozenset</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>])<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">frozenset</span>({<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">3</span>})

因为不可修改,所以没有像set那样的addpop方法


31 转为集合类型

返回一个set对象,集合内不允许有重复元素:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">159</span>]: a <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">1</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">160</span>]: <span style="color: rgb(111, 66, 193);">set</span>(a)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">160</span>]: {<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">4</span>}</p><p><br  /></p>

32 转为切片对象

class slice(startstop[, step])

返回一个表示由 range(start, stop, step) 所指定索引集的 slice对象,它让代码可读性、可维护性变好。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: a <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">1</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: my_slice_meaning <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">slice</span>(<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">2</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: a[my_slice_meaning]<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: [<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(0, 92, 197);">1</span>]</p><p><br  /></p>

33 转元组

tuple() 将对象转为一个不可变的序列类型

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">16</span>]: i_am_list <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">5</span>]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">17</span>]: i_am_tuple <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">tuple</span>(i_am_list)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">18</span>]: i_am_tuple<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">18</span>]: (<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">3</span>, <span style="color: rgb(0, 92, 197);">5</span>)</p>

五、 类和对象

34 是否可调用  

检查对象是否可被调用

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">callable</span>(str)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">True</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">callable</span>(int)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(0, 92, 197);">True</span>
<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">18</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />    ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />    ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />    ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />    ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  />    ...<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">19</span>]: xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(<span style="color: rgb(3, 47, 98);">'001'</span>,<span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">20</span>]: <span style="color: rgb(111, 66, 193);">callable</span>(xiaoming)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">20</span>]: <span style="color: rgb(0, 92, 197);">False</span>

如果能调用xiaoming(), 需要重写Student类的__call__方法:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />    ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />    ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />    ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />    ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__call__</span>(self):<br  />    ...:         <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">'I can be called'</span>)<br  />    ...:         <span style="color: rgb(111, 66, 193);">print</span>(<span style="color: rgb(3, 47, 98);">f'my name is <span style="color: rgb(36, 41, 46);">{self.name}</span>'</span>)<br  />    ...:<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: t <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(<span style="color: rgb(3, 47, 98);">'001'</span>,<span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">t</span>()<br  /><span style="color: rgb(227, 98, 9);">I</span> can be called<br  /><p>my name <span style="color: rgb(0, 92, 197);">is</span> xiaoming</p><p><br  /></p>

35 ascii 展示对象  

调用对象的 __repr__ 方法,获得该方法的返回值,如下例子返回值为字符串

<span style="color: rgb(0, 92, 197);">>>></span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(227, 98, 9);">Student</span>():<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />        self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />        self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />        <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name

调用:

<span style="color: rgb(0, 92, 197);">>>></span> xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(id<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'1'</span>,name<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><span style="color: rgb(0, 92, 197);">>>></span> xiaoming<br  />id <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">1</span>, name <span style="color: rgb(0, 92, 197);">=</span> xiaoming<br  /><span style="color: rgb(0, 92, 197);">>>></span> <span style="color: rgb(111, 66, 193);">ascii</span>(xiaoming)<br  /><p><span style="color: rgb(3, 47, 98);">'id = 1, name = xiaoming'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

36 类方法 

classmethod 装饰器对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />    ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />    ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />    ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />    ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  />    ...:     @classmethod<br  />    ...:     def <span style="color: rgb(111, 66, 193);">f</span>(cls):<br  /><p>...:         <span style="color: rgb(111, 66, 193);">print</span>(cls)</p><p><br  /></p>

37 动态删除属性  

删除对象的属性

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">delattr</span>(xiaoming,<span style="color: rgb(3, 47, 98);">'id'</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">hasattr</span>(xiaoming,<span style="color: rgb(3, 47, 98);">'id'</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(0, 92, 197);">False</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

38 一键查看对象所有方法 

不带参数时返回当前范围内的变量、方法和定义的类型列表;带参数时返回参数的属性,方法列表。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">96</span>]: <span style="color: rgb(111, 66, 193);">dir</span>(xiaoming)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">96</span>]:<br  />[<span style="color: rgb(3, 47, 98);">'__class__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__delattr__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__dict__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__dir__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__doc__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__eq__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__format__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__ge__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__getattribute__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__gt__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__hash__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__init__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__init_subclass__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__le__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__lt__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__module__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__ne__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__new__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__reduce__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__reduce_ex__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__repr__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__setattr__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__sizeof__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__str__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__subclasshook__'</span>,<br  /> <span style="color: rgb(3, 47, 98);">'__weakref__'</span>,<br  /> <br  /><p><span style="color: rgb(3, 47, 98);">'name'</span>]</p><p><br  /></p>

39 动态获取对象属性 

获取对象的属性

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />   ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />   ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />   ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />   ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />   ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(id<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'001'</span>,name<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">getattr</span>(xiaoming,<span style="color: rgb(3, 47, 98);">'name'</span>) <span style="color: rgb(106, 115, 125);"># 获取xiaoming这个实例的name属性值</span><br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(3, 47, 98);">'xiaoming'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

40 对象是否有这个属性

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />   ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />   ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />   ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />   ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />   ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(id<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'001'</span>,name<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">hasattr</span>(xiaoming,<span style="color: rgb(3, 47, 98);">'name'</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);">True</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">hasattr</span>(xiaoming,<span style="color: rgb(3, 47, 98);">'address'</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(0, 92, 197);">False</span></p><p><span style="color:#e36209;">、</span></p>

41 对象门牌号 

返回对象的内存地址

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">id</span>(xiaoming)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">98234208</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

42 isinstance

判断object是否为类classinfo的实例,是返回true

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />   ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />   ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />   ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />   ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />   ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(id<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'001'</span>,name<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">isinstance</span>(xiaoming,<span style="color: rgb(227, 98, 9);">Student</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);">True</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

43 父子关系鉴定

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(111, 66, 193);">undergraduate</span>(<span style="color: rgb(227, 98, 9);">Student</span>):<br  />    ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">studyClass</span>(self):<br  />    ...:         pass<br  />    ...:     def <span style="color: rgb(111, 66, 193);">attendActivity</span>(self):<br  />    ...:         pass<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">issubclass</span>(undergraduate,<span style="color: rgb(227, 98, 9);">Student</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(0, 92, 197);">True</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">issubclass</span>(object,<span style="color: rgb(227, 98, 9);">Student</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);">False</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">issubclass</span>(<span style="color: rgb(227, 98, 9);">Student</span>,object)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(0, 92, 197);">True</span>

如果class是classinfo元组中某个元素的子类,也会返回True

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">issubclass</span>(int,(int,float))<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">True</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

44 所有对象之根

object 是所有类的基类

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: o <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">object</span>()<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">type</span>(o)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: object</p><p><br  /></p>

45 创建属性的两种方式

返回 property 属性,典型的用法:

<span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(227, 98, 9);">C</span>:<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self):<br  />        self._x <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">None</span><br  /><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">getx</span>(self):<br  />        <span style="color: rgb(215, 58, 73);">return</span> self._x<br  /><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">setx</span>(self, value):<br  />        self._x <span style="color: rgb(0, 92, 197);">=</span> value<br  /><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">delx</span>(self):<br  />        <span style="color: rgb(215, 58, 73);">del</span> self._x<br  />    <span style="color: rgb(106, 115, 125);"># 使用property类创建 property 属性</span><br  />    x <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">property</span>(getx, setx, delx, <span style="color: rgb(3, 47, 98);">"I'm the 'x' property."</span>)

使用python装饰器,实现与上完全一样的效果代码:

<span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(227, 98, 9);">C</span>:<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self):<br  />        self._x <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">None</span><br  /><br  />    <span style="color: rgb(111, 66, 193);">@property</span><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">x</span>(self):<br  />        <span style="color: rgb(215, 58, 73);">return</span> self._x<br  /><br  />    <span style="color: rgb(111, 66, 193);">@x.setter</span><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">x</span>(self, value):<br  />        self._x <span style="color: rgb(0, 92, 197);">=</span> value<br  /><br  />    <span style="color: rgb(111, 66, 193);">@x.deleter</span><br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">x</span>(self):<br  /><p><span style="color: rgb(215, 58, 73);">del</span> self._x</p><p><br  /></p>

46 查看对象类型

class type(namebasesdict)

传入一个参数时,返回 object 的类型:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>():<br  />   ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,id,name):<br  />   ...:         self.id <span style="color: rgb(0, 92, 197);">=</span> id<br  />   ...:         self.name <span style="color: rgb(0, 92, 197);">=</span> name<br  />   ...:     def <span style="color: rgb(111, 66, 193);">__repr__</span>(self):<br  />   ...:         <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(3, 47, 98);">'id = '</span><span style="color: rgb(0, 92, 197);">+</span>self.id <span style="color: rgb(0, 92, 197);">+</span><span style="color: rgb(3, 47, 98);">', name = '</span><span style="color: rgb(0, 92, 197);">+</span>self.name<br  />   ...:<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(id<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'001'</span>,name<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'xiaoming'</span>)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">type</span>(xiaoming)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: __main__.<span style="color: rgb(227, 98, 9);">Student</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: <span style="color: rgb(111, 66, 193);">type</span>(<span style="color: rgb(111, 66, 193);">tuple</span>())<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">4</span>]: tuple</p><p><br  /></p>

47 元类

xiaomingxiaohongxiaozhang 都是学生,这类群体叫做 Student.

Python 定义类的常见方法,使用关键字 class

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">36</span>]: class <span style="color: rgb(227, 98, 9);">Student</span>(object):<br  />    ...:     pass

xiaomingxiaohongxiaozhang 是类的实例,则:

xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>()<br  />xiaohong <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>()<br  />xiaozhang <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>()

创建后,xiaoming 的 __class__ 属性,返回的便是 Student

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">38</span>]: xiaoming.__class__<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">38</span>]: __main__.<span style="color: rgb(227, 98, 9);">Student</span>

问题在于,Student 类有 __class__属性,如果有,返回的又是什么?

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">39</span>]: xiaoming.__class__.__class__<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">39</span>]: type

哇,程序没报错,返回 type

那么,我们不妨猜测:Student 类,类型就是 type

换句话说,Student类就是一个对象,它的类型就是 type

所以,Python 中一切皆对象,类也是对象

Python 中,将描述 Student 类的类被称为:元类。

按照此逻辑延伸,描述元类的类被称为:元元类,开玩笑了~ 描述元类的类也被称为元类。

聪明的朋友会问了,既然 Student 类可创建实例,那么 type 类可创建实例吗?如果能,它创建的实例就叫:类 了。你们真聪明!

说对了,type 类一定能创建实例,比如 Student 类了。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">40</span>]: <span style="color: rgb(227, 98, 9);">Student</span> <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">type</span>(<span style="color: rgb(3, 47, 98);">'Student'</span>,(),{})<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">41</span>]: <span style="color: rgb(227, 98, 9);">Student</span><br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">41</span>]: __main__.<span style="color: rgb(227, 98, 9);">Student</span>

它与使用 class 关键字创建的 Student 类一模一样。

Python 的类,因为又是对象,所以和 xiaomingxiaohong 对象操作相似。支持:

  • 赋值

  • 拷贝

  • 添加属性

  • 作为函数参数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">43</span>]: <span style="color: rgb(227, 98, 9);">StudentMirror</span> <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span> <span style="color: rgb(106, 115, 125);"># 类直接赋值 # 类直接赋值</span><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">44</span>]: <span style="color: rgb(227, 98, 9);">Student</span>.class_property <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">'class_property'</span> <span style="color: rgb(106, 115, 125);"># 添加类属性</span><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">46</span>]: <span style="color: rgb(111, 66, 193);">hasattr</span>(<span style="color: rgb(227, 98, 9);">Student</span>, <span style="color: rgb(3, 47, 98);">'class_property'</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">46</span>]: <span style="color: rgb(0, 92, 197);">True</span>

元类,确实使用不是那么多,也许先了解这些,就能应付一些场合。就连 Python 界的领袖 Tim Peters 都说:

“元类就是深度的魔法,99%的用户应该根本不必为此操心。


六、工具

48 枚举对象  

返回一个可以枚举的对象,该对象的next()方法将返回一个元组。

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: s <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(3, 47, 98);">"a"</span>,<span style="color: rgb(3, 47, 98);">"b"</span>,<span style="color: rgb(3, 47, 98);">"c"</span>]<br  />    ...: for i ,v <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">enumerate</span>(s,<span style="color: rgb(0, 92, 197);">1</span>):<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(i,v)<br  />    ...:<br  /><span style="color: rgb(0, 92, 197);">1</span> a<br  /><span style="color: rgb(0, 92, 197);">2</span> b<br  /><p><span style="color: rgb(0, 92, 197);">3</span> c</p><p><br  /></p>

49 查看变量所占字节数

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: import sys<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: a <span style="color: rgb(0, 92, 197);">=</span> {<span style="color: rgb(3, 47, 98);">'a'</span>:<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(3, 47, 98);">'b'</span>:<span style="color: rgb(0, 92, 197);">2.0</span>}<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: sys.<span style="color: rgb(111, 66, 193);">getsizeof</span>(a) <span style="color: rgb(106, 115, 125);"># 占用240个字节</span><br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);">240</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

50 过滤器  

在函数中设定过滤条件,迭代元素,保留返回值为True的元素:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: fil <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">filter</span>(<span style="color: rgb(215, 58, 73);">lambda</span> x: x<span style="color: rgb(0, 92, 197);">>10</span>,[<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">11</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">45</span>,<span style="color: rgb(0, 92, 197);">7</span>,<span style="color: rgb(0, 92, 197);">6</span>,<span style="color: rgb(0, 92, 197);">13</span>])<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">list</span>(fil)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: [<span style="color: rgb(0, 92, 197);">11</span>, <span style="color: rgb(0, 92, 197);">45</span>, <span style="color: rgb(0, 92, 197);">13</span>]</p><p><br  /></p>

51 返回对象的哈希值  

返回对象的哈希值,值得注意的是自定义的实例都是可哈希的,listdictset等可变对象都是不可哈希的(unhashable)

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">hash</span>(xiaoming)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(0, 92, 197);">6139638</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">hash</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>])<br  /><p><span style="color: rgb(106, 115, 125);"># TypeError: unhashable type: 'list'</span></p><p><span style="color: rgb(106, 115, 125);"><br  /></span></p>

52 一键帮助 

返回对象的帮助文档

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">help</span>(xiaoming)<br  /><span style="color: rgb(227, 98, 9);">Help</span> on <span style="color: rgb(227, 98, 9);">Student</span> <span style="color: rgb(0, 92, 197);">in</span> module __main__ object:<br  /><br  />class <span style="color: rgb(227, 98, 9);">Student</span>(builtins.object)<br  /> <span style="color: rgb(0, 92, 197);">|</span>  <span style="color: rgb(227, 98, 9);">Methods</span> defined here:<br  /> <span style="color: rgb(0, 92, 197);">|</span><br  /> <span style="color: rgb(0, 92, 197);">|</span>  <span style="color: rgb(111, 66, 193);">__init__</span>(self, id, name)<br  /> <span style="color: rgb(0, 92, 197);">|</span><br  /> <span style="color: rgb(0, 92, 197);">|</span>  <span style="color: rgb(111, 66, 193);">__repr__</span>(self)<br  /> <span style="color: rgb(0, 92, 197);">|</span><br  /> <span style="color: rgb(0, 92, 197);">|</span>  <span style="color: rgb(227, 98, 9);">Data</span> descriptors defined here:<br  /> <span style="color: rgb(0, 92, 197);">|</span><br  /> <span style="color: rgb(0, 92, 197);">|</span>  __dict__<br  /> <span style="color: rgb(0, 92, 197);">|</span>      dictionary <span style="color: rgb(215, 58, 73);">for</span> instance <span style="color: rgb(111, 66, 193);">variables</span> (if defined)<br  /> <span style="color: rgb(0, 92, 197);">|</span><br  /> <span style="color: rgb(0, 92, 197);">|</span>  __weakref__<br  /><p><span style="color: rgb(0, 92, 197);">|</span>      list of weak references to the <span style="color: rgb(111, 66, 193);">object</span> (if defined)</p><p><br  /></p>

53 获取用户输入 

获取用户输入内容

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">input</span>()<br  />aa<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(3, 47, 98);">'aa'</span></p><p><span style="color: rgb(3, 47, 98);"><br  /></span></p>

54 创建迭代器类型

使用iter(obj, sentinel), 返回一个可迭代对象, sentinel可省略(一旦迭代到此元素,立即终止)

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: lst <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">5</span>]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: for i <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">iter</span>(lst):<br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(i)<br  />    ...:<br  /><span style="color: rgb(0, 92, 197);">1</span><br  /><span style="color: rgb(0, 92, 197);">3</span><br  /><span style="color: rgb(0, 92, 197);">5</span>
<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: class <span style="color: rgb(227, 98, 9);">TestIter</span>(object):<br  />    ...:     <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self):<br  />    ...:         self.l<span style="color: rgb(0, 92, 197);">=</span>[<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">5</span>]<br  />    ...:         self.i<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(111, 66, 193);">iter</span>(self.l)<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__call__</span>(self):  <span style="color: rgb(106, 115, 125);">#定义了__call__方法的类的实例是可调用的</span><br  />    ...:         item <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">next</span>(self.i)<br  />    ...:         <span style="color: rgb(111, 66, 193);">print</span> (<span style="color: rgb(3, 47, 98);">"__call__ is called,fowhich would return"</span>,item)<br  />    ...:         return item<br  />    ...:     def <span style="color: rgb(111, 66, 193);">__iter__</span>(self): <span style="color: rgb(106, 115, 125);">#支持迭代协议(即定义有__iter__()函数)</span><br  />    ...:         <span style="color: rgb(111, 66, 193);">print</span> (<span style="color: rgb(3, 47, 98);">"__iter__ is called!!"</span>)<br  />    ...:         <span style="color: rgb(111, 66, 193);">return</span> iter(self.l)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: t <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">TestIter</span>()<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">t</span>() <span style="color: rgb(106, 115, 125);"># 因为实现了__call__,所以t实例能被调用</span><br  />__call__ <span style="color: rgb(0, 92, 197);">is</span> called,which would <span style="color: rgb(215, 58, 73);">return</span> <span style="color: rgb(0, 92, 197);">1</span><br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(0, 92, 197);">1</span><br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: for e <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(227, 98, 9);">TestIter</span>(): <span style="color: rgb(106, 115, 125);"># 因为实现了__iter__方法,所以t能被迭代</span><br  />    ...:     <span style="color: rgb(111, 66, 193);">print</span>(e)<br  />    ...:<br  />__iter__ <span style="color: rgb(0, 92, 197);">is</span> called!!<br  /><span style="color: rgb(0, 92, 197);">1</span><br  /><span style="color: rgb(0, 92, 197);">3</span><br  /><span style="color: rgb(0, 92, 197);">2</span><br  /><span style="color: rgb(0, 92, 197);">3</span><br  /><span style="color: rgb(0, 92, 197);">4</span><br  /><p><span style="color: rgb(0, 92, 197);">5</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

55 打开文件

返回文件对象

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: fo <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">open</span>(<span style="color: rgb(3, 47, 98);">'D:/a.txt'</span>,mode<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'r'</span>, encoding<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(3, 47, 98);">'utf-8'</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: fo.<span style="color: rgb(111, 66, 193);">read</span>()<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(3, 47, 98);">'ufefflife is not so long,nI use Python to play.'</span>

mode取值表:

字符 意义
'r' 读取(默认)
'w' 写入,并先截断文件
'x' 排它性创建,如果文件已存在则失败
'a' 写入,如果文件存在则在末尾追加
'b' 二进制模式
't' 文本模式(默认)
'+' 打开用于更新(读取与写入)

56 创建range序列

  1. range(stop)

  2. range(start, stop[,step])

生成一个不可变序列:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">11</span>)<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">1</span>]: <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">0</span>, <span style="color: rgb(0, 92, 197);">11</span>)<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">0</span>,<span style="color: rgb(0, 92, 197);">11</span>,<span style="color: rgb(0, 92, 197);">1</span>)<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">2</span>]: <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">0</span>, <span style="color: rgb(0, 92, 197);">11</span>)</p><p><br  /></p>

57 反向迭代器

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: rev <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">reversed</span>([<span style="color: rgb(0, 92, 197);">1</span>,<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">1</span>])<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: for i <span style="color: rgb(0, 92, 197);">in</span> rev:<br  />     ...:     <span style="color: rgb(111, 66, 193);">print</span>(i)<br  />     ...:<br  /><span style="color: rgb(0, 92, 197);">1</span><br  /><span style="color: rgb(0, 92, 197);">3</span><br  /><span style="color: rgb(0, 92, 197);">2</span><br  /><span style="color: rgb(0, 92, 197);">4</span><br  /><p><span style="color: rgb(0, 92, 197);">1</span></p><p><span style="color: rgb(0, 92, 197);"><br  /></span></p>

58 聚合迭代器

创建一个聚合了来自每个可迭代对象中的元素的迭代器:

<span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">1</span>]: x <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">3</span>,<span style="color: rgb(0, 92, 197);">2</span>,<span style="color: rgb(0, 92, 197);">1</span>]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">2</span>]: y <span style="color: rgb(0, 92, 197);">=</span> [<span style="color: rgb(0, 92, 197);">4</span>,<span style="color: rgb(0, 92, 197);">5</span>,<span style="color: rgb(0, 92, 197);">6</span>]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">3</span>]: <span style="color: rgb(111, 66, 193);">list</span>(<span style="color: rgb(111, 66, 193);">zip</span>(y,x))<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">3</span>]: [(<span style="color: rgb(0, 92, 197);">4</span>, <span style="color: rgb(0, 92, 197);">3</span>), (<span style="color: rgb(0, 92, 197);">5</span>, <span style="color: rgb(0, 92, 197);">2</span>), (<span style="color: rgb(0, 92, 197);">6</span>, <span style="color: rgb(0, 92, 197);">1</span>)]<br  /><br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">4</span>]: a <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">range</span>(<span style="color: rgb(0, 92, 197);">5</span>)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">5</span>]: b <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(111, 66, 193);">list</span>(<span style="color: rgb(3, 47, 98);">'abcde'</span>)<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">6</span>]: b<br  /><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">6</span>]: [<span style="color: rgb(3, 47, 98);">'a'</span>, <span style="color: rgb(3, 47, 98);">'b'</span>, <span style="color: rgb(3, 47, 98);">'c'</span>, <span style="color: rgb(3, 47, 98);">'d'</span>, <span style="color: rgb(3, 47, 98);">'e'</span>]<br  /><span style="color: rgb(227, 98, 9);">In</span> [<span style="color: rgb(0, 92, 197);">7</span>]: [<span style="color: rgb(111, 66, 193);">str</span>(y) <span style="color: rgb(0, 92, 197);">+</span> <span style="color: rgb(111, 66, 193);">str</span>(x) <span style="color: rgb(215, 58, 73);">for</span> x,y <span style="color: rgb(0, 92, 197);">in</span> <span style="color: rgb(111, 66, 193);">zip</span>(a,b)]<br  /><p><span style="color: rgb(227, 98, 9);">Out</span>[<span style="color: rgb(0, 92, 197);">7</span>]: [<span style="color: rgb(3, 47, 98);">'a0'</span>, <span style="color: rgb(3, 47, 98);">'b1'</span>, <span style="color: rgb(3, 47, 98);">'c2'</span>, <span style="color: rgb(3, 47, 98);">'d3'</span>, <span style="color: rgb(3, 47, 98);">'e4'</span>]</p><p><br  /></p>

59 链式操作

<span style="color: rgb(215, 58, 73);">from</span> operator <span style="color: rgb(215, 58, 73);">import</span> (add, sub)<br  /><br  /><br  /><span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">add_or_sub</span>(a, b, oper):<br  />    <span style="color: rgb(215, 58, 73);">return</span> (add <span style="color: rgb(215, 58, 73);">if</span> oper <span style="color: rgb(0, 92, 197);">==</span> <span style="color: rgb(3, 47, 98);">'+'</span> <span style="color: rgb(215, 58, 73);">else</span> sub)(a, b)<br  /><br  /><br  /><p><span style="color: rgb(111, 66, 193);">add_or_sub</span>(<span style="color: rgb(0, 92, 197);">1</span>, <span style="color: rgb(0, 92, 197);">2</span>, <span style="color: rgb(3, 47, 98);">'-'</span>)  <span style="color: rgb(106, 115, 125);"># -1</span></p><p><span style="color: rgb(106, 115, 125);"><br  /></span></p>

60 对象序列化

对象序列化,是指将内存中的对象转化为可存储或传输的过程。很多场景,直接一个类对象,传输不方便。

但是,当对象序列化后,就会更加方便,因为约定俗成的,接口间的调用或者发起的 web 请求,一般使用 json 串传输。

实际使用中,一般对类对象序列化。先创建一个 Student 类型,并创建两个实例。

<span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(227, 98, 9);">Student</span>():<br  />    <span style="color: rgb(215, 58, 73);">def</span> <span style="color: rgb(111, 66, 193);">__init__</span>(self,<span style="color: rgb(0, 92, 197);">**</span>args):<br  />        self.ids <span style="color: rgb(0, 92, 197);">=</span> args[<span style="color: rgb(3, 47, 98);">'ids'</span>]<br  />        self.name <span style="color: rgb(0, 92, 197);">=</span> args[<span style="color: rgb(3, 47, 98);">'name'</span>]<br  />        self.address <span style="color: rgb(0, 92, 197);">=</span> args[<span style="color: rgb(3, 47, 98);">'address'</span>]<br  />xiaoming <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(ids <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">1</span>,name <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">'xiaoming'</span>,address <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">'北京'</span>)<br  />xiaohong <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(227, 98, 9);">Student</span>(ids <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(0, 92, 197);">2</span>,name <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">'xiaohong'</span>,address <span style="color: rgb(0, 92, 197);">=</span> <span style="color: rgb(3, 47, 98);">'南京'</span>)

导入 json 模块,调用 dump 方法,就会将列表对象 [xiaoming,xiaohong],序列化到文件 json.txt 中。

<span style="color: rgb(215, 58, 73);">import</span> json<br  /><br  /><span style="color: rgb(215, 58, 73);">with</span> <span style="color: rgb(111, 66, 193);">open</span>(<span style="color: rgb(3, 47, 98);">'json.txt'</span>, <span style="color: rgb(3, 47, 98);">'w'</span>) <span style="color: rgb(215, 58, 73);">as</span> f:<br  />    json.<span style="color: rgb(111, 66, 193);">dump</span>([xiaoming,xiaohong], f, default<span style="color: rgb(0, 92, 197);">=</span><span style="color: rgb(215, 58, 73);">lambda</span> obj: obj.__dict__, ensure_ascii<span style="color: rgb(0, 92, 197);">=False</span>, indent<span style="color: rgb(0, 92, 197);">=2</span>, sort_keys<span style="color: rgb(0, 92, 197);">=True</span>)

生成的文件内容,如下:

[<br  />    {<br  />        <span style="color: rgb(3, 47, 98);">"address"</span>:<span style="color: rgb(3, 47, 98);">"北京"</span>,<br  />        <span style="color: rgb(3, 47, 98);">"ids"</span>:<span style="color: rgb(0, 92, 197);">1</span>,<br  />        <span style="color: rgb(3, 47, 98);">"name"</span>:<span style="color: rgb(3, 47, 98);">"xiaoming"</span><br  />    },<br  />    {<br  />        <span style="color: rgb(3, 47, 98);">"address"</span>:<span style="color: rgb(3, 47, 98);">"南京"</span>,<br  />        <span style="color: rgb(3, 47, 98);">"ids"</span>:<span style="color: rgb(0, 92, 197);">2</span>,<br  />        <span style="color: rgb(3, 47, 98);">"name"</span>:<span style="color: rgb(3, 47, 98);">"xiaohong"</span><br  />    }<br  />]
<section style="letter-spacing: 0px;white-space: normal;color: rgb(0, 0, 0);text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;line-height: 1.5em;"><br  /></section><p><br  /></p><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding-right: 10px;padding-left: 10px;max-width: 100%;font-size: 16px;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: -apple-system, system-ui, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-style="background-color: rgb(255, 255, 255); letter-spacing: 0.544px; font-size: 16px; text-align: center; color: rgba(230, 230, 230, 0.9) !important;" class="js_darkmode__45" data-darkmode-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" style="max-width: 100%;letter-spacing: 0.544px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;color: rgba(230, 230, 230, 0.9) !important;"><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding: 10px;max-width: 100%;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;font-size: 16px;font-weight: 700;letter-spacing: 0.544px;widows: 1;word-spacing: 1px;caret-color: rgb(51, 51, 51);color: rgb(63, 63, 63);line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-role="outer" label="Powered by 135editor.com" data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(168, 168, 168)" data-darkmode-original-color="rgb(62, 62, 62)" data-style="max-width: 100%; font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; white-space: normal; background-color: rgb(255, 255, 255); color: rgb(62, 62, 62); text-align: left; box-sizing: border-box !important; overflow-wrap: break-word !important;" style="max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;color: rgb(62, 62, 62);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-role="outer" label="Powered by 135editor.com" data-mpa-powered-by="yiban.io" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-role="outer" label="Powered by 135editor.com" style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-size: 16px;font-family: 微软雅黑, "Helvetica Nue", sans-serif;word-spacing: 1.6px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-style="background-color: rgb(255, 255, 255); color: rgba(230, 230, 230, 0.9); letter-spacing: 0.544px; text-size-adjust: auto; font-size: 16px; text-align: center; word-spacing: 1.6px;" data-darkmode-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-bgcolor-15862411819306="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862411819306="rgb(255, 255, 255)" data-darkmode-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15862671987026="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862671987026="rgb(255, 255, 255)" data-darkmode-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864118999603="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864118999603="rgb(255, 255, 255)" data-darkmode-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864940858736="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864940858736="rgb(255, 255, 255)" data-darkmode-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691402="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691402="rgb(255, 255, 255)" data-darkmode-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691739="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691739="rgb(255, 255, 255)" data-darkmode-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456075="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456075="rgb(255, 255, 255)" data-darkmode-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456615="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456615="rgb(255, 255, 255)" data-darkmode-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15886839320558="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15886839320558="rgb(255, 255, 255)" data-darkmode-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-color-159923607914210="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-159923607914210="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-159923607914210="rgb(25, 25, 25)" data-darkmode-original-bgcolor-159923607914210="rgb(255, 255, 255)" data-darkmode-bgcolor-160008070860010="rgb(25, 25, 25)" data-darkmode-original-bgcolor-160008070860010="rgb(255, 255, 255)" data-darkmode-color-160008070860010="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-160008070860010="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16072664870629="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16072664870629="rgb(255, 255, 255)" data-darkmode-color-16072664870629="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16072664870629="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16073544711184="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16073544711184="rgb(255, 255, 255)" data-darkmode-color-16073544711184="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16073544711184="rgba(230, 230, 230, 0.9)" style="max-width: 100%;letter-spacing: 0.544px;word-spacing: 1.6px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-style="background-color: rgb(255, 255, 255); color: rgba(230, 230, 230, 0.9); letter-spacing: 0.544px; text-size-adjust: auto; font-size: 16px; text-align: center; word-spacing: 1.6px;" data-darkmode-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-bgcolor-15862411819306="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862411819306="rgb(255, 255, 255)" data-darkmode-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15862671987026="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862671987026="rgb(255, 255, 255)" data-darkmode-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864118999603="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864118999603="rgb(255, 255, 255)" data-darkmode-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864940858736="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864940858736="rgb(255, 255, 255)" data-darkmode-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691402="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691402="rgb(255, 255, 255)" data-darkmode-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691739="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691739="rgb(255, 255, 255)" data-darkmode-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456075="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456075="rgb(255, 255, 255)" data-darkmode-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456615="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456615="rgb(255, 255, 255)" data-darkmode-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15886839320558="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15886839320558="rgb(255, 255, 255)" data-darkmode-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-color-159923607914210="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-159923607914210="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-159923607914210="rgb(25, 25, 25)" data-darkmode-original-bgcolor-159923607914210="rgb(255, 255, 255)" data-darkmode-bgcolor-160008070860010="rgb(25, 25, 25)" data-darkmode-original-bgcolor-160008070860010="rgb(255, 255, 255)" data-darkmode-color-160008070860010="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-160008070860010="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16072664870629="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16072664870629="rgb(255, 255, 255)" data-darkmode-color-16072664870629="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16072664870629="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16073544711184="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16073544711184="rgb(255, 255, 255)" data-darkmode-color-16073544711184="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16073544711184="rgba(230, 230, 230, 0.9)" style="max-width: 100%;letter-spacing: 0.544px;word-spacing: 1.6px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="__bg_gif" data-ratio="0.08658008658008658"  data-type="gif" data-w="462" data-width="100%" style="color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;display: block;box-sizing: border-box !important;overflow-wrap: break-word !important;visibility: visible !important;width: 578px !important;" src="https://www.zkxjob.com/wp-content/uploads/2022/07/wxsync-2022-07-ae45c8833e6e59cceba3c2fda2df37d1.gif"  /></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p>


推荐阅读:

入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径


干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影


趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!


AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影


小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!


年度爆款文案

  • 1).卧槽!Pdf转Word用Python轻松搞定

  • 2).学Python真香!我用100行代码做了个网站,帮人PS旅行图片,赚个鸡腿吃

  • 3).首播过亿,火爆全网,我分析了《乘风破浪的姐姐》,发现了这些秘密 

  • 4).80行代码!用Python做一个哆来A梦分身 

  • 5).你必须掌握的20个python代码,短小精悍,用处无穷 

  • 6).30个Python奇淫技巧集 

  • 7).我总结的80页《菜鸟学Python精选干货.pdf》,都是干货 

  • 8).再见Python!我要学Go了!2500字深度分析!

  • 9).发现一个舔狗福利!这个Python爬虫神器太爽了,自动下载妹子图片


<section data-mpa-template="t" mpa-from-tpl="t" data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(168, 168, 168)" data-darkmode-original-color-15923650965579="rgb(62, 62, 62)" data-style="color: rgb(62, 62, 62); font-size: 15px; letter-spacing: 0.544px; background-color: rgb(255, 255, 255); font-family: monospace; text-align: left; widows: 1; word-spacing: 2px; caret-color: rgb(255, 0, 0); white-space: pre-wrap;" style="max-width: 100%;font-size: 15px;letter-spacing: 0.544px;word-spacing: 2px;caret-color: rgb(255, 0, 0);color: rgb(62, 62, 62);white-space: pre-wrap;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section mpa-from-tpl="t" data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(168, 168, 168)" data-darkmode-original-color-15923650965579="rgb(62, 62, 62)" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(167, 167, 167)" data-darkmode-original-color-15923650965579="rgb(63, 63, 63)" data-style="letter-spacing: 0.544px; font-size: 16px; color: rgb(63, 63, 63); word-spacing: 1px; line-height: inherit;" style="max-width: 100%;letter-spacing: 0.544px;font-size: 16px;color: rgb(63, 63, 63);word-spacing: 1px;line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-mpa-template-id="1250" data-mpa-color="#ffffff" data-mpa-category="divider" data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(230, 230, 230)" data-darkmode-original-color="rgb(0, 0, 0)" data-style="margin-right: 0.5em; margin-left: 0.5em; white-space: normal; font-family: -apple-system-font, system-ui, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; color: rgb(0, 0, 0); letter-spacing: 0px; word-spacing: 2px;" data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(230, 230, 230)" data-darkmode-original-color-15923650965579="rgb(0, 0, 0)" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(167, 167, 167)" data-darkmode-original-color-15923650965579="rgb(63, 63, 63)" data-style="letter-spacing: 0.544px; font-size: 16px; color: rgb(63, 63, 63); word-spacing: 1px; line-height: inherit;" style="max-width: 100%;letter-spacing: 0.544px;line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-mpa-template-id="1250" data-mpa-color="#ffffff" data-mpa-category="divider" data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(230, 230, 230)" data-darkmode-original-color="rgb(0, 0, 0)" data-style="margin-right: 0.5em; margin-left: 0.5em; white-space: normal; font-family: -apple-system-font, system-ui, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; color: rgb(0, 0, 0); letter-spacing: 0px; word-spacing: 2px;" data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(230, 230, 230)" data-darkmode-original-color-15923650965579="rgb(0, 0, 0)" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(230, 230, 230)" data-darkmode-original-color="rgb(0, 0, 0)" data-darkmode-bgcolor-15923650965579="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15923650965579="rgb(255, 255, 255)" data-darkmode-color-15923650965579="rgb(230, 230, 230)" data-darkmode-original-color-15923650965579="rgb(0, 0, 0)" style="margin-right: 0em;margin-left: 0em;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-right: 0.5em;margin-left: 0.5em;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(168, 168, 168)" data-darkmode-original-color="rgb(62, 62, 62)" data-style="letter-spacing: 0.544px; font-weight: 700; text-align: -webkit-center; background-color: rgb(255, 255, 255); font-size: 16px; color: rgb(62, 62, 62); widows: 1; word-spacing: 2px;" style="max-width: 100%;letter-spacing: 0.544px;text-align: center;color: rgb(62, 62, 62);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-color="rgb(168, 168, 168)" data-darkmode-original-color="rgb(62, 62, 62)" data-style="letter-spacing: 0.544px; font-weight: 700; text-align: -webkit-center; background-color: rgb(255, 255, 255); font-size: 16px; color: rgb(62, 62, 62); widows: 1; word-spacing: 2px;" style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;letter-spacing: 0.544px;text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><h1 style="max-width: 100%;white-space: pre-wrap;letter-spacing: 0.544px;font-family: 微软雅黑;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;letter-spacing: 0.544px;line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-mpa-template="t" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-id="94086" data-color="#276ca3" data-tools="135编辑器" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;color: rgb(63, 63, 63);letter-spacing: 0.544px;line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-mpa-template="t" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-id="94086" data-color="#276ca3" data-tools="135编辑器" mpa-from-tpl="t" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section mpa-from-tpl="t" style="max-width: 100%;display: inline-block;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;letter-spacing: 0.544px;line-height: inherit;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-style="letter-spacing: 0.544px; background-color: rgb(255, 255, 255); text-align: center; color: rgba(230, 230, 230, 0.9); font-size: 16px; line-height: 25.6px; overflow-wrap: break-word !important;" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15882396318564="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15900529136199="rgba(230, 230, 230, 0.9)" style="max-width: 100%;letter-spacing: 0.544px;text-align: center;color: rgba(230, 230, 230, 0.9);line-height: 25.6px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" style="max-width: 100%;display: inline-block;clear: both;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-tools="135编辑器" data-id="91842" data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" style="max-width: 100%;letter-spacing: 0.544px;border-width: 0px;border-style: none;border-color: initial;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" style="max-width: 100%;display: inline-block;clear: both;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-brushtype="text" data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgimage-15860613985508="1" data-style="padding: 18px 15px 20px 10px; color: rgb(86, 146, 214); text-align: center; letter-spacing: 1.5px; background-image: url('https://www.zkxjob.com/wp-content/uploads/2022/07/wxsync-2022-07-a2a8a5e1e58f30392066a170034ee027.png'); background-size: 100% 100%; background-repeat: no-repeat; overflow-wrap: break-word !important;" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgimage-15870356070738="1" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgimage-15870356071023="1" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgimage-15882384789136="1" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgimage-15882396318564="1" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" data-darkmode-bgimage-15900529136199="1" style="padding: 18px 15px 20px 10px;max-width: 100%;background-size: 100% 100%;background-image: url('https://www.zkxjob.com/wp-content/uploads/2022/07/wxsync-2022-07-a2a8a5e1e58f30392066a170034ee027.png');color: rgb(86, 146, 214);letter-spacing: 1.5px;background-repeat: no-repeat;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgimage-15860613985508="1" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgimage-15870356070738="1" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgimage-15870356071023="1" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgimage-15882384789136="1" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgimage-15882396318564="1" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" data-darkmode-bgimage-15900529136199="1" style="max-width: 100%;display: flex;justify-content: center;align-items: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(230, 230, 230)" data-darkmode-original-color-15860613985508="rgb(0, 0, 0)" data-darkmode-bgimage-15860613985508="1" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(230, 230, 230)" data-darkmode-original-color-15870356070738="rgb(0, 0, 0)" data-darkmode-bgimage-15870356070738="1" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(230, 230, 230)" data-darkmode-original-color-15870356071023="rgb(0, 0, 0)" data-darkmode-bgimage-15870356071023="1" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(230, 230, 230)" data-darkmode-original-color-15882384789136="rgb(0, 0, 0)" data-darkmode-bgimage-15882384789136="1" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(230, 230, 230)" data-darkmode-original-color-15882396318564="rgb(0, 0, 0)" data-darkmode-bgimage-15882396318564="1" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(230, 230, 230)" data-darkmode-original-color-15900529136199="rgb(0, 0, 0)" data-darkmode-bgimage-15900529136199="1" style="margin-left: 2px;max-width: 100%;width: 20px;box-sizing: border-box !important;overflow-wrap: break-word !important;"></section><section data-brushtype="text" data-darkmode-bgcolor-15860613985508="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15860613985508="rgb(255, 255, 255)" data-darkmode-color-15860613985508="rgb(51, 51, 51)" data-darkmode-original-color-15860613985508="rgb(51, 51, 51)" data-darkmode-bgimage-15860613985508="1" data-darkmode-bgcolor-15870356070738="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356070738="rgb(255, 255, 255)" data-darkmode-color-15870356070738="rgb(51, 51, 51)" data-darkmode-original-color-15870356070738="rgb(51, 51, 51)" data-darkmode-bgimage-15870356070738="1" data-darkmode-bgcolor-15870356071023="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15870356071023="rgb(255, 255, 255)" data-darkmode-color-15870356071023="rgb(51, 51, 51)" data-darkmode-original-color-15870356071023="rgb(51, 51, 51)" data-darkmode-bgimage-15870356071023="1" data-darkmode-bgcolor-15882384789136="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882384789136="rgb(255, 255, 255)" data-darkmode-color-15882384789136="rgb(51, 51, 51)" data-darkmode-original-color-15882384789136="rgb(51, 51, 51)" data-darkmode-bgimage-15882384789136="1" data-darkmode-bgcolor-15882396318564="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15882396318564="rgb(255, 255, 255)" data-darkmode-color-15882396318564="rgb(51, 51, 51)" data-darkmode-original-color-15882396318564="rgb(51, 51, 51)" data-darkmode-bgimage-15882396318564="1" data-darkmode-bgcolor-15900529136199="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15900529136199="rgb(255, 255, 255)" data-darkmode-color-15900529136199="rgb(51, 51, 51)" data-darkmode-original-color-15900529136199="rgb(51, 51, 51)" data-darkmode-bgimage-15900529136199="1" style="max-width: 100%;font-size: 14px;color: rgb(51, 51, 51);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-family: 楷体, 楷体_GB2312, SimKai;white-space: pre-wrap;font-size: 12px;box-sizing: border-box !important;overflow-wrap: break-word !important;">点阅读原文,领星球优惠卷!</span></section></section></section></section></section></section></section>

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

你可能也喜欢

热评文章

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享