forked from 521xueweihan/521xueweihan.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-build-in-func.html
888 lines (746 loc) · 57.6 KB
/
python-build-in-func.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>削微寒 - Python内建方法学习笔记</title>
<meta name="description" content="">
<meta name="author" content="xueweihan">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://www.xueweihan.com/theme/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="http://www.xueweihan.com/theme/bootstrap.min.css" rel="stylesheet">
<link href="http://www.xueweihan.com/theme/bootstrap.min.responsive.css" rel="stylesheet">
<link href="http://www.xueweihan.com/theme/local.css" rel="stylesheet">
<link href="http://www.xueweihan.com/theme/pygments.css" rel="stylesheet">
<!-- So Firefox can bookmark->"abo this site" -->
<link href="http://www.xueweihan.com/feeds/all.atom.xml" rel="alternate" title="削微寒" type="application/atom+xml">
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://www.xueweihan.com">削微寒</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="http://www.xueweihan.com/pages/my-book-life.html">我的书单</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<div class="content">
<div class="row">
<div class="span9">
<div class='article'>
<div class="content-title">
<h1>Python内建方法学习笔记</h1>
2016-03-19 22:01
by <a class="url fn" href="http://www.xueweihan.com/author/xueweihan.html">xueweihan</a>
</div>
<div><h1>撸一遍python的内建方法</h1>
<p>这样做的好处就是:我如果要完成一个功能的时候,如果能用内建方法完成,就用内建方法。这样可以提高效率,同时使自己的代码更加优雅。哎呦?那岂不是撸完就是python高手了?我先帮大家试试效果,请静候我的反馈!</p>
<p>反馈:内建方法全部看完了,并敲完所有的内建方法,收获还是有的,但是发现不了解的知识更多了。内建方法属于python的标准库中的一章。python的标准库中还有很多值得一看的章节。<a href="http://python.usyiyi.cn/python_278/library/index.html">python2.7.8中文标准库文档</a>,这些内容我粗略的看了一遍。这个内建方法的学习笔记我周三就写完了,本来想在写内建类型的学习笔记。但是发现太多了!而且我发现,看的太多记下的东西就会变少,所以我打算把重心转移到我自己练手的项目中(现在还没什么值得分享的东西,等拿得出手肯定会告诉大家的)。我想把学习标准库中收获的技巧用到我的项目中,这样学以致用才能真正融汇贯通。这就是我的下一步学习计划:通过实际项目,运用python标准库中的技巧!</p>
<p>结论就是:看完标准库我还没有成为高手,但是我收获了很多知识,基础牢固了一些。下一步打算在我自己的项目中运用这些技巧,提高自己的能力和技术!</p>
<p>相关资料
- <a href="https://docs.python.org/2/library/functions.html#all" title="官方文档-英文">官方文档-内建方法(英文)-2.7.11</a>
- <a href="http://python.usyiyi.cn/python_278/library/functions.html" title="官方文档-中文">官方文档-内建方法(中文)-2.7.8</a></p>
<h2>内建方法</h2>
<table>
<thead>
<tr>
<th>常用内建方法</th>
<th></th>
<th></th>
<th></th>
<th align="left"></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#all">all</a></td>
<td><a href="#divmod">divmod</a></td>
<td><a href="#input">input</a></td>
<td><a href="#open">open</a></td>
<td align="left"><a href="#staticmethod">staticmethod</a></td>
</tr>
<tr>
<td><a href="#abs">abs</a></td>
<td><a href="#enumerate">enumerate</a></td>
<td><a href="#int">int</a></td>
<td><a href="#ord">ord</a></td>
<td align="left"><a href="#str">str</a></td>
</tr>
<tr>
<td><a href="#any">any</a></td>
<td><a href="#eval">eval</a></td>
<td><a href="#isinstance">isinstance</a></td>
<td><a href="#pow">pow</a></td>
<td align="left"><a href="#sum">sum</a></td>
</tr>
<tr>
<td><a href="#basestring">basestring</a></td>
<td><a href="#execfile">execfile</a></td>
<td><a href="#issubclass">issubclass</a></td>
<td><a href="#print">print</a></td>
<td align="left"><a href="#super">super</a></td>
</tr>
<tr>
<td><a href="#bin">bin</a></td>
<td><a href="#file">file</a></td>
<td><a href="#iter">iter</a></td>
<td><a href="#property">property</a></td>
<td align="left"><a href="#tuple">tuple</a></td>
</tr>
<tr>
<td><a href="#bool">bool</a></td>
<td><a href="#filter">filter</a></td>
<td><a href="#len">len</a></td>
<td><a href="#range">range</a></td>
<td align="left"><a href="#type">type</a></td>
</tr>
<tr>
<td><a href="#bytearray">bytearray</a></td>
<td><a href="#float">float</a></td>
<td><a href="#list">list</a></td>
<td><a href="#raw_input">raw_input</a></td>
<td align="left"><a href="#unichr">unichr</a></td>
</tr>
<tr>
<td><a href="#callable">callable</a></td>
<td><a href="#format">format</a></td>
<td><a href="#locals">locals</a></td>
<td><a href="#reduce">reduce</a></td>
<td align="left"><a href="#unicode">unicode</a></td>
</tr>
<tr>
<td><a href="#chr">chr</a></td>
<td><a href="#frozenset">frozenset</a></td>
<td><a href="#long">long</a></td>
<td><a href="#reload">reload</a></td>
<td align="left"><a href="#vars">vars</a></td>
</tr>
<tr>
<td><a href="#classmethod">classmethod</a></td>
<td><a href="#getattr">getattr</a></td>
<td><a href="#map">map</a></td>
<td><a href="#repr">repr</a></td>
<td align="left"><a href="#xrange">xrange</a></td>
</tr>
<tr>
<td><a href="#cmp">cmp</a></td>
<td><a href="#globals">globals</a></td>
<td><a href="#max">max</a></td>
<td><a href="#reversed">reversed</a></td>
<td align="left"><a href="#zip">zip</a></td>
</tr>
<tr>
<td><a href="#compile">compile</a></td>
<td><a href="#hasattr">hasattr</a></td>
<td><a href="#memoryview">memoryview</a></td>
<td><a href="#round">round</a></td>
<td align="left"><a href="#__import__"><strong>import</strong></a></td>
</tr>
<tr>
<td><a href="#complex">complex</a></td>
<td><a href="#hash">hash</a></td>
<td><a href="#min">min</a></td>
<td><a href="#set">set</a></td>
<td align="left"><a href="#apply">apply</a></td>
</tr>
<tr>
<td><a href="#delattr">delattr</a></td>
<td><a href="#help">help</a></td>
<td><a href="#next">next</a></td>
<td><a href="#setattr">setattr</a></td>
<td align="left"><a href="#buffer">buffer</a></td>
</tr>
<tr>
<td><a href="#dict">dict</a></td>
<td><a href="#hex">hex</a></td>
<td><a href="#object">object</a></td>
<td><a href="#slice">slice</a></td>
<td align="left"><a href="#coerce">coerce</a></td>
</tr>
<tr>
<td><a href="#dir">dir</a></td>
<td><a href="#id">id</a></td>
<td><a href="#oct">oct</a></td>
<td><a href="#sorted">sorted</a></td>
<td align="left"><a href="#intern">intern</a></td>
</tr>
</tbody>
</table>
<p><strong>说明</strong>:<code>[,xxx]</code>表示为可选参数。</p>
<p><span id="abs"></span></p>
<h4>abs(x)</h4>
<p>返回x的绝对值,例如:</p>
<div class="highlight"><pre><span></span><span class="nb">abs</span><span class="p">(</span><span class="o">-</span><span class="mf">1.23</span><span class="p">)</span> <span class="c1"># 1.23</span>
</pre></div>
<p><span id="all"></span></p>
<h4>all(iterable)</h4>
<p>如果iterable(迭代对象)的所有元素为真(或者iterable为空:[],'',()等),返回True。例如:</p>
<div class="highlight"><pre><span></span><span class="nb">all</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">all</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># False</span>
</pre></div>
<p><span id="any"></span></p>
<h4>any(iterable)</h4>
<p>如果iterable中只要又一个元素为真,就返回True。例如:</p>
<div class="highlight"><pre><span></span><span class="nb">any</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># True</span>
</pre></div>
<p><span id="basestring"></span></p>
<h4>basestring()</h4>
<p>它是str和unicode的超类,不能被调用或者实例化。只能用来测试一个对象是不是str或unicode的实例。例如:</p>
<div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="s1">u'a'</span>
<span class="n">b</span> <span class="o">=</span> <span class="s1">'b'</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span> <span class="c1"># True</span>
</pre></div>
<p><span id="bin"></span></p>
<h4>bin()</h4>
<p>将<strong>整数</strong>转成二进制<strong>字符串</strong>。</p>
<div class="highlight"><pre><span></span><span class="nb">bin</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># '0b11'</span>
</pre></div>
<p><span id="bool"></span></p>
<h4>bool()</h4>
<p>将一个值转化成布尔值。</p>
<div class="highlight"><pre><span></span><span class="nb">bool</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">bool</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">bool</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="c1"># False</span>
<span class="nb">bool</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">bool</span><span class="p">([])</span> <span class="c1"># False</span>
</pre></div>
<p><span id="bytearray"></span></p>
<h4>bytearray()</h4>
<p>结合memoryview,实现改变str类型对象的值。请移步<a href="http://python.usyiyi.cn/python_278/library/stdtypes.html#memoryview-type">python内建类型</a></p>
<p><span id="callable"></span></p>
<h4>callable(object)</h4>
<p>如果object参数可调用,返回True;否则返回False。对类的调用,返回一个新的实例。对于实例,如果实例有<strong>call</strong>方法,则该实例也是可以调用的。例如:</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TestCallable</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nb">callable</span><span class="p">(</span><span class="n">TestCallable</span><span class="p">)</span> <span class="c1"># True</span>
<span class="nb">callable</span><span class="p">(</span><span class="n">TestCallable</span><span class="p">())</span> <span class="c1"># True</span>
</pre></div>
<p><span id="chr"></span></p>
<h4>chr(i)</h4>
<p>返回一个字符,该字符的<a href="http://www.asciitable.com/" title="ASCII表">ASCII</a>码为整数i。i的取值范围:0 <= x < 255</p>
<div class="highlight"><pre><span></span><span class="nb">chr</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="c1"># 'd'</span>
<span class="nb">ord</span><span class="p">(</span><span class="s1">'d'</span><span class="p">)</span> <span class="c1"># 100</span>
</pre></div>
<p><span id="classmethod"></span></p>
<h4>classmethod(functions)</h4>
<p>将传入的方法包装成类方法(类方法是指,类不需要实力化就可以直接调用的方法)。
类方法的第一个参数必须为类(约定俗称用cls代表),实例方法的第一个参数必须为实例(约定俗称为self),这两种接收参数的方法叫做:隐式第一参数(implicit first argument)。静态方法(通过@staticmethod装饰的方法)不需要如上述两个方法的隐式参数。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
<p>注意:通过@classmethod修饰的方法,为类方法。类可以直接调用(如C.f());实例也可以直接调用(如C().f()),切记类方法中不能操作实例的属性。如果子类调用类方法,子类对象被传递为隐式的第一个参数(也就是cls为子类对象)。</p>
<p><span id="cmp"></span></p>
<h4>cmp(x, y)</h4>
<p>比较两个对象x和y,当<code>x < y</code>返回'-1';<code>x > y</code>返回'1';<code>x == y</code>返回0</p>
<div class="highlight"><pre><span></span><span class="n">cmo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># -1</span>
<span class="nb">cmp</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 1</span>
<span class="nb">cmp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 0</span>
</pre></div>
<p>注意:<code>bool(-1)</code>结果为True</p>
<p><span id="compile"></span></p>
<h4>compile(source, filename, mode)</h4>
<p>compile可以将字符串或者Unicode字符串编译成代码对象。代码对象可以通过<code>exec</code>或<code>eval</code>执行。
- 参数source:字符串或者AST(Abstract Syntax Trees)对象。
- 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
- 参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。</p>
<div class="highlight"><pre><span></span><span class="n">source</span> <span class="o">=</span><span class="s2">"if cmp(1, 1)==0: print 'yes'"</span>
<span class="n">test_compile</span> <span class="o">=</span> <span class="nb">compile</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="s1">''</span><span class="p">,</span> <span class="s1">'exec'</span><span class="p">)</span>
<span class="k">exec</span><span class="p">(</span><span class="n">test_compile</span><span class="p">)</span> <span class="c1">#yes</span>
</pre></div>
<p>注意:source是字符串,要注意引号和双引号,最后指定什么model,就用那种方法执行。compile返回的类型是<code>code</code>。</p>
<p><span id="complex"></span></p>
<h4>complex(str)</h4>
<p>创建一个复数。</p>
<div class="highlight"><pre><span></span><span class="nb">complex</span><span class="p">(</span><span class="s1">'1+2j'</span><span class="p">)</span> <span class="c1">#可行</span>
<span class="nb">complex</span><span class="p">(</span><span class="s1">'1 + 2j'</span><span class="p">)</span> <span class="c1">#报错(因为字符串中'+'或者'-'两遍不能有空白)</span>
</pre></div>
<p><span id="delattr"></span></p>
<h4>delattr(object, name)</h4>
<p>参数是一个对象和一个字符串(<code>name</code>为字符串类型),字符串必须是该对象的某个属性名。效果是:删除该对象的name对应的属性。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'XueWeiHan'</span>
<span class="n">test_delatter</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
<span class="n">test_delatter</span><span class="o">.</span><span class="n">name</span> <span class="c1"># XueWeiHan</span>
<span class="nb">delattr</span><span class="p">(</span><span class="n">test_delatter</span><span class="p">,</span> <span class="s1">'name'</span><span class="p">)</span>
<span class="n">test_delatter</span><span class="o">.</span><span class="n">name</span> <span class="c1"># 报错,没有name属性,删除成功</span>
</pre></div>
<p><span id="dict"></span></p>
<h4>dict()</h4>
<p>创建一个新字典。</p>
<div class="highlight"><pre><span></span><span class="nb">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># {'a': 1}</span>
</pre></div>
<p><span id="dir"></span></p>
<h3>dir(object)</h3>
<p>如果没有参数,返回当前本地作用域的名字列表。如果有参数,返回该参数的属性列表(属性和方法)。
如果类中定义了__dir__方法,则<code>dir()</code>会先默认从__dict__属性中收集信息。</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="nb">type</span><span class="p">(</span><span class="n">time</span><span class="p">)</span> <span class="c1"># model</span>
<span class="nb">dir</span><span class="p">(</span><span class="n">time</span><span class="p">)</span> <span class="c1"># ['__doc__','__file__',...,'sleep','strftime',]就是time.可以调用的属性和方法</span>
<span class="k">class</span> <span class="nc">TestDir</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__dir__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="s1">'TestDir1'</span><span class="p">,</span> <span class="s1">'TestDir2'</span><span class="p">]</span>
<span class="nb">dir</span><span class="p">(</span><span class="n">TestDir</span><span class="p">)</span> <span class="c1"># ['TestDir1', 'TestDir2']</span>
</pre></div>
<p>注意:dir()主要是方便交互环境</p>
<p><span id="divmod"></span></p>
<h3>divmod(a, b)</h3>
<p>效果:a除以b,返回商和余数的二元组(两个数组成的元组)</p>
<div class="highlight"><pre><span></span><span class="nb">divmod</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#(2, 1)</span>
</pre></div>
<p><span id="enumerate"></span></p>
<h3>enumerate(sequence, start=0)</h3>
<p>返回一个枚举对象。sequence必须是序列,迭代器iterator,或者支持迭代的对象:</p>
<blockquote>
<p>迭代器Iterators:
迭代器仅是一容器对象,它实现了迭代器协议。它有两个基本方法:
1)next方法
返回容器的下一个元素
2)__iter__方法
返回迭代器自身</p>
</blockquote>
<div class="highlight"><pre><span></span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Xuweihan'</span><span class="p">,</span> <span class="s1">'James'</span><span class="p">,</span> <span class="s1">'Kobe'</span><span class="p">]</span>
<span class="n">enumerate_data</span> <span class="o">=</span> <span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="c1">#这是个枚举类型数据</span>
<span class="nb">list</span><span class="p">(</span><span class="n">enumerate_data</span><span class="p">)</span> <span class="c1">#[(0, 'Xuweihan'), (1, 'James'), (2, 'Kobe')]</span>
<span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="c1">#[(1, 'Xuweihan'), (2, 'James'), (3, 'Kobe')]</span>
<span class="c1"># 实现</span>
<span class="k">def</span> <span class="nf">enumerate</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="n">n</span> <span class="o">=</span><span class="n">start</span>
<span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">:</span>
<span class="k">yield</span> <span class="n">n</span><span class="p">,</span> <span class="n">elem</span>
<span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
<p><strong>注意</strong>:enumerate是生成器,惰性计算。可以调用next()</p>
<p><span id="eval"></span></p>
<h3>eval(expression[,globals()[,locals()]])</h3>
<p>expression参数被当作python表达式执行。使用<code>globals()</code>和<code>locals()</code>指定执行的代码中变量是全局变量还是本地变量。代码例子如下:</p>
<div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1">#全局变量</span>
<span class="k">def</span> <span class="nf">add_one</span><span class="p">():</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1">#本地变量</span>
<span class="nb">eval</span><span class="p">(</span><span class="s1">'a+1'</span><span class="p">,</span> <span class="nb">globals</span><span class="p">())</span> <span class="c1">#结果为 2</span>
<span class="nb">eval</span><span class="p">(</span><span class="s1">'a+1'</span><span class="p">,</span> <span class="nb">locals</span><span class="p">())</span> <span class="c1">#结果为 3</span>
</pre></div>
<p><span id="execfile"></span></p>
<h3>execfile(filename[,globals[,locals]])</h3>
<p>该函数类似exec(上面的那个),不同的是他解析一个<strong>文件</strong>,而不是字符串。它不同与__import__语句的地方在于,它不使用模块管理——它无条件的读入文件且不会创建一个新模块。
(<strong>不常用</strong>)</p>
<p><span id="file"></span></p>
<h3>file()</h3>
<p>file类型的构造函数。打开一个文件的时候,建议使用<a href="#open">open()</a>而不使用<code>file()</code>。<code>file</code>更适合类型检测例如:<code>isinstance(f, file)</code>。</p>
<p><span id="filter"></span></p>
<h3>filter(functions, iterable)</h3>
<p>构造一个列表,列表的元素来自于iterable,返回对于这些元素function返回<code>True</code>的元素。iterable可以是个序列,支持迭代的容器或者一个迭代器。
filter函数相当于过滤,返回符合functions的列表中的元素。带判断的列表生成式:<code>[i for i in list if i]</code></p>
<div class="highlight"><pre><span></span><span class="n">test_list</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'python'</span><span class="p">,</span> <span class="s1">'ruby'</span><span class="p">,</span> <span class="s1">'node.js'</span><span class="p">]</span>
<span class="n">result</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">((</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">==</span><span class="s1">'python'</span><span class="p">),</span> <span class="n">test_list</span><span class="p">)</span> <span class="err">#结果:</span><span class="p">[</span><span class="s1">'python'</span><span class="p">]</span>
</pre></div>
<p><span id="float"></span></p>
<h3>float()</h3>
<p>将字符串或者数字转化成浮点数。</p>
<div class="highlight"><pre><span></span><span class="nb">float</span><span class="p">(</span><span class="s1">'.3333'</span><span class="p">)</span> <span class="c1">#0.3333</span>
<span class="nb">float</span><span class="p">(</span><span class="s1">'a.333'</span><span class="p">)</span> <span class="c1">#报错</span>
<span class="nb">float</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1">#1.0</span>
</pre></div>
<p><span id="format"></span></p>
<h3>format(value[,format_spec])</h3>
<p>将value转化成“格式化”的表现形式,格式由<code>format_spec</code>控制。</p>
<div class="highlight"><pre><span></span><span class="n">_string</span> <span class="o">=</span> <span class="s1">'{name} is a pythoner'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'Xueweihan'</span><span class="p">)</span>
<span class="k">print</span> <span class="n">_string</span> <span class="c1"># 'Xueweihan is a pythoner'</span>
</pre></div>
<p><span id="frozenset"></span></p>
<h3>frozenset()</h3>
<p>返回一个新的forzenset对象。就是一个不可变的集合,所以存在哈希值,可以作为字典的key。
<strong>注意</strong>:python的集合类型不支持整数。</p>
<p><span id="getattr"></span></p>
<h3>getattr(object, name[,default])</h3>
<p>返回object的属性值。name必须是个字符串。如果名字指明的属性不存在,则返回default参数。</p>
<p>例如:<code>getattr(x, 'test')</code>等于x.test。</p>
<p><span id="globals"></span></p>
<h3>globals()</h3>
<p><code>globals(x)</code>,x成为全局变量。</p>
<p><span id="hasattr"></span></p>
<h3>hasattr(object, name)</h3>
<p>参数是一个对象和一个字符串。如果对象含有该属性则返回True;否则返回False。</p>
<p><span id="hash"></span></p>
<h3>hash(object)</h3>
<p>返回对象的hash值。hash值是整数,它被用于字典查找时快速比较字典的键。相同的数值有相同的hash(例如:1和1.0的hash值相同)</p>
<p><span id="help"></span></p>
<h3>help([object])</h3>
<p>调用帮助系统(主要用于交互式的使用过程中)。如果没有指定object的话,则进入交互式的help帮助系统。</p>
<p><span id="hex"></span></p>
<h3>hex()</h3>
<p>将number类型的数据,转化成“0x”打头小写的十六进制字符串:</p>
<div class="highlight"><pre><span></span><span class="nb">hex</span><span class="p">(</span><span class="mi">33</span><span class="p">)</span> <span class="c1">#'-0x21'</span>
<span class="c1">#float类型数据</span>
<span class="nb">float</span><span class="o">.</span><span class="n">hex</span><span class="p">(</span><span class="mf">0.32</span><span class="p">)</span> <span class="c1">#'0x1.47ae147ae147bp-2'</span>
</pre></div>
<p><span id="id"></span></p>
<h3>id(object)</h3>
<p>返回对象的“表示”,这是一个整数,在对象的生命期内<strong>唯一且不变</strong>。<br />
<strong>注意</strong>:CPython中:id就是对象的内存地址。</p>
<div class="highlight"><pre><span></span><span class="nb">id</span><span class="p">(</span><span class="mi">33</span><span class="p">)</span> <span class="c1"># 140300932661128</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">33</span>
<span class="nb">id</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1"># 140300932661128</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">33</span>
<span class="nb">id</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1"># 140300932661128</span>
</pre></div>
<p><span id="input"></span></p>
<h3>input()</h3>
<p>获取用户的输入。</p>
<p>建议用:raw_input</p>
<p><span id="int"></span></p>
<h3>int(x, base=10)</h3>
<p>将数字或字符串x转化成一个整数,如果没有参数则返回0。</p>
<p><span id="isinstance"></span></p>
<h3>isinstance(object, classinfo)</h3>
<p>如果参数object是参数classinfo的一个实例;或者是一个子类的实例,最终返回真。</p>
<p>推荐使用这个而不是用type进行判断。</p>
<div class="highlight"><pre><span></span><span class="nb">isinstance</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="c1">#True</span>
</pre></div>
<p><span id="issubclass"></span></p>
<h3>issubclass(class, classinfo)</h3>
<p>如果class是classinfo的子类,则返回真。</p>
<p><span id="iter"></span></p>
<h3>iter(o[,sentinel])</h3>
<p>返回一个iterator(迭代器)对象。如果没有第二个参数,o必须是个集合独享,要么支持迭代协议<a href="#enumerate">参考</a>,要么支持序列协议。例如:</p>
<div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]:</span>
<span class="k">pass</span>
<span class="c1">#等同于</span>
<span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
<span class="k">break</span>
</pre></div>
<p>如果有第二个参数sentinel,o必须是个可调用对象。使用场景:</p>
<div class="highlight"><pre><span></span><span class="c1"># 读取一个文件的行,直到读到特定行</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'test.txt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">iter</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">readline</span><span class="p">,</span> <span class="s1">''</span><span class="p">):</span>
<span class="n">process_line</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
<p><span id="len"></span></p>
<h3>len(s)</h3>
<p>返回对象的长度(元素的个数)。s可以是:序列或者集合。</p>
<div class="highlight"><pre><span></span><span class="nb">len</span><span class="p">(</span><span class="s1">'xueweihan'</span><span class="p">)</span> <span class="c1">#9</span>
</pre></div>
<p><span id="list"></span></p>
<h3>list([iterable])</h3>
<p>返回一个列表,其中的元素来自于iterable。iterable可以是个序列,支持迭代的容器,或者迭代器对象。</p>
<div class="highlight"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="s1">'xueweihan'</span><span class="p">)</span> <span class="c1">#['x', 'u', 'e', 'w', 'e', 'i', 'h', 'a', 'n']</span>
</pre></div>
<p><span id="locals"></span></p>
<h3>locals()</h3>
<p>把传入的变量,修饰成局部变量。</p>
<p><span id="long"></span></p>
<h3>long()</h3>
<p>将一个字符串或者数字传化成一个长整数。</p>
<p><span id="map"></span></p>
<h3>map()</h3>
<p>遍历iterable的每个元素,并把元素作为参数传入function,返回结果的列表。</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">add_two</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
<span class="k">return</span> <span class="n">num</span><span class="o">+</span><span class="mi">2</span>
<span class="n">num_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">]</span>
<span class="nb">map</span><span class="p">(</span><span class="n">add_two</span><span class="p">,</span> <span class="n">num_list</span><span class="p">)</span> <span class="c1">#[3,4,5,6,7]</span>
</pre></div>
<p><span id="max"></span></p>
<h3>max()</h3>
<p>返回可迭代的对象中最大的元素。</p>
<p><span id="memoryview"></span></p>
<h3>memoryview()</h3>
<p>返回memoryview对象,它允许Python代码访问对象的内部数据而不用复制,只要该对象支持缓冲区协议。
如有疑问请参考<a href="http://python.usyiyi.cn/python_278/library/stdtypes.html#memoryview-type">python内建类型memoryview</a></p>
<p><span id="min"></span></p>
<h3>min()</h3>
<p>返回可迭代的对象中的最小的元素。</p>
<p><span id="next"></span></p>
<h3>next(iterator[,default])</h3>
<p>通过调用iterator(迭代器)的next()方法,得到它的下一个元素。如果有default参数,在迭代器迭代完之后返回该参数;否则抛出StopIteration。</p>
<div class="highlight"><pre><span></span><span class="n">test_next_data</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="k">print</span> <span class="nb">next</span><span class="p">(</span><span class="n">test_next_data</span><span class="p">,</span> <span class="s1">'Done'</span><span class="p">)</span> <span class="c1">#1</span>
<span class="k">print</span> <span class="nb">next</span><span class="p">(</span><span class="n">test_next_data</span><span class="p">,</span> <span class="s1">'Done'</span><span class="p">)</span> <span class="c1">#2</span>
<span class="k">print</span> <span class="nb">next</span><span class="p">(</span><span class="n">test_next_data</span><span class="p">,</span> <span class="s1">'Done'</span><span class="p">)</span> <span class="c1">#3</span>
<span class="k">print</span> <span class="nb">next</span><span class="p">(</span><span class="n">test_next_data</span><span class="p">,</span> <span class="s1">'Done'</span><span class="p">)</span> <span class="c1">#Done</span>
</pre></div>
<p><strong>注意</strong>:通过<code>iter()</code>返回的就是迭代器。</p>
<p><span id="object"></span></p>
<h3>object()</h3>
<p>object是所有新式类的基类</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">pass</span>
<span class="c1">#继承于object的类为新式类</span>
</pre></div>
<p><span id="oct"></span></p>
<h3>oct()</h3>
<p>将任意一个整数转成一个八进制字符串。</p>
<p><span id="open"></span></p>
<h3>open(name, [,mode[,buffering]])</h3>
<p>打开文件的方法,返回一个file类型对象。如果文件不能打开抛出IOError。<br />
mode:用什么方式打开文件。'r'读文件;'w'写文件;'a'附加。如果没有mode,默认是'r'。
buffering: 缓冲</p>
<p><span id="ord"></span></p>
<h3>ord()</h3>
<p>参考:<a href="#chr">chr()</a></p>
<p><strong>注意</strong>:如果是unicode,则返回unicode码</p>
<p><span id="pow"></span></p>
<h3>pow(x, y[,z])</h3>
<p>返回x的y次幂:<code>x**y</code><br />
如果有z参数:<code>(x**y) % z</code></p>
<p><span id="print"></span></p>
<h3>print()</h3>
<p>这个方法可以输出内容到file对象。<br />
<strong>注意</strong>:不常用,为了使print语句失效,而是用print()函数。(print和print()不是一个东西)可以在你的模块上面使用future语句:<code>from __future__ import print_function</code></p>
<p><span id="property"></span></p>
<h3>property()</h3>
<p>property其实就是个控制属性的权限的方法。同时实现,经property装饰的方法,可通过<code>Object.xxx</code>调用属性,把实例方法,变成实例的属性。这样做的好处是:可以在方法中实现限制条件,同时限制可执行的操作。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="bp">None</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span>
<span class="nd">@name.setter</span>
<span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">if</span> <span class="n">value</span> <span class="o">!=</span> <span class="s1">'xueweihan'</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">value</span>
<span class="nd">@name.deleter</span>
<span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">print</span> <span class="s1">'del name!'</span>
<span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_name</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">Student</span><span class="p">()</span>
<span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'aaa'</span>
<span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="c1">#赋值成功'aaa'</span>
<span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'xueweihan'</span>
<span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="c1">#赋值失败'aaa'</span>
<span class="k">del</span> <span class="n">s</span><span class="o">.</span><span class="n">name</span> <span class="c1">#‘del name!’</span>
</pre></div>
<p><strong>注意</strong>:@property可以控制属性,比如只读属性:不实现@xxx.setter和@xxx.deleter就可以了。</p>
<p><span id="range"></span></p>
<h3>range(start, stop[,step])</h3>
<p>用于创建列表,‘要头不要尾’。setp参数为步长</p>
<div class="highlight"><pre><span></span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1">#[0, 1, 2, 3, 4]</span>
<span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="c1">#[2, 3]</span>
<span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="c1">#[10, 15]</span>
</pre></div>
<p><span id="raw_input"></span></p>
<h3>raw_input()</h3>
<p>获取控制台的输入</p>
<p><span id="reduce"></span></p>
<h3>reduce(function,iterable[,initializer])</h3>
<p>将带有两个参数的function累计地应用到iterable的元素上,从左向右。如果提供可选的参数initializer,它在计算时放在可迭代序列的最前面,并且当可迭代序列为空时作为默认值。例如:</p>
<div class="highlight"><pre><span></span><span class="nb">reduce</span><span class="p">((</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="c1">#相当于计算(((1+2)+3)+4)</span>
</pre></div>
<p><span id="reload"></span></p>
<h3>reload(module)</h3>
<p>如果你重新修改了模块,并且不打算停止重新启动python解释器的情况下使用该模块的最新版本。那么就使用<code>reload()</code>,在reload之前就<strong>需要确保import过</strong>。</p>
<div class="highlight"><pre><span></span><span class="c1"># test_reload.py</span>
<span class="k">print</span> <span class="s1">'a'</span>
<span class="kn">import</span> <span class="nn">test_reload</span> <span class="c1">#'a'</span>
<span class="c1">#修改test_reload.py ——> print 'b'</span>
<span class="kn">import</span> <span class="nn">test_reload</span> <span class="c1">#什么都输出,因为没有之前已经import过了,所以没有重新加载</span>
<span class="nb">reload</span><span class="p">(</span><span class="n">test_reload</span><span class="p">)</span> <span class="c1">#'b'重新加载成功!</span>
</pre></div>
<p><span id="repr"></span></p>
<h3>repr(object)</h3>
<p>精准的返回某个对象可打印形式的字符串。返回的值,可以通过eval()执行。</p>
<div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="nb">repr</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="c1">#"'a'"</span>
</pre></div>
<p><span id="reversed"></span></p>
<h3>reversed(seq)</h3>
<p>返回一个反向的<strong>迭代器</strong>。seq必须是一个具有__reversed__()方法或支持序列协议的对象(实现__len__()和__getitem__()方法)。</p>
<div class="highlight"><pre><span></span><span class="n">test_reverse</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span> <span class="c1">#<listreverseiterator object at 0x10bcab810></span>
<span class="n">test_reverse</span><span class="o">.</span><span class="n">next</span> <span class="c1">#4</span>
</pre></div>
<p><strong>注意</strong>:可以编写一个定制的__reversed__()方法的可能。</p>
<p><span id="round"></span></p>
<h3>round(number[, ndigits])</h3>
<p>返回一个浮点数的近似值,保留小数点后<code>ndigits</code>位,默认<code>ndigits</code>为零。这个方法不好用,因为近似值不是<strong>四舍五入</strong>。</p>
<div class="highlight"><pre><span></span><span class="nb">round</span><span class="p">(</span><span class="mf">2.675</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="c1">#2.67</span>
</pre></div>
<p><span id="set"></span></p>
<h3>set([iterable])</h3>
<p>返回一个集合对象,iterable是可迭代的对象。</p>
<p><span id="setattr"></span></p>
<h3>setattr(object,name,value)</h3>
<p>给object的属性赋值,可以是存在的属性,也可以是不存的属性。例如:<code>setattr(s, 'name','xueweihan')</code>等同于<code>s.name='xueweihan'</code></p>
<p><span id="slice"></span></p>
<h3>slice()</h3>
<p>常用的切片方法:</p>
<div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="c1">#2</span>
</pre></div>
<p><span id="sorted"></span></p>
<h3>sorted(iterable[,cmp[,key[,reverse]]])</h3>
<p>用于iterable对象排序的方法。
- cmp指定一个自定义的带有两个参数的比较函数(可迭代的元素),它应该根据第一个参数是小于、等于还是大于第二个参数返回负数、零或者正数:cmp=lambda x,y: cmp(x.lower(), y.lower())。默认值是None。
- key指定一个带有一个参数的函数,它用于从每个列表元素选择一个比较的关键字:key=str.lower。默认值是None(直接比较元素)。
- reverse是一个布尔值。如果设置为True,那么列表元素以反向比较排序。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Student</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">grade</span><span class="p">,</span> <span class="n">age</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">grade</span> <span class="o">=</span> <span class="n">grade</span>
<span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">repr</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">grade</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">age</span><span class="p">))</span>
<span class="n">student_objects</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">Student</span><span class="p">(</span><span class="s1">'john'</span><span class="p">,</span> <span class="s1">'A'</span><span class="p">,</span> <span class="mi">15</span><span class="p">),</span>
<span class="n">Student</span><span class="p">(</span><span class="s1">'jane'</span><span class="p">,</span> <span class="s1">'B'</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="n">Student</span><span class="p">(</span><span class="s1">'dave'</span><span class="p">,</span> <span class="s1">'B'</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">]</span>
<span class="nb">sorted</span><span class="p">(</span><span class="n">student_objects</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">student</span><span class="p">:</span> <span class="n">student</span><span class="o">.</span><span class="n">age</span><span class="p">)</span> <span class="c1"># sort by age</span>
<span class="c1">#结果为:[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]</span>
</pre></div>
<p><span id="staticmethod"></span></p>
<h3>staticmethod(function)</h3>
<p>返回一个静态方法。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
<p><strong>注意</strong>:静态方法基可以在类上调用<code>C.f()</code>,也可以在实例上调用<code>C().f()</code>。</p>
<p><span id="str"></span></p>
<h3>str(object='')</h3>
<p>返回一个字符串,包含对象的友好可打印表达形式。<br />
<strong>注意</strong>:<code>print</code>调用的就是对象的__str__方法</p>
<p><span id="sum"></span></p>
<h3>sum(iterable[,start])</h3>
<p>将iterable的元素从左向右相加并返回总和。</p>
<div class="highlight"><pre><span></span><span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="c1">#6</span>
<span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#8</span>
</pre></div>
<p><span id="super"></span></p>
<h3>super()</h3>
<p>用于显性的指定父类的方法。同时实现方法的绑定。</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">B</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="c1">#C的父类实例的method()</span>
</pre></div>
<p><span id="tuple"></span></p>
<h3>tuple([iterable])</h3>
<p>返回一个元素,元素顺序和iterable的元素相同。</p>
<p><span id="tpye"></span></p>
<h3>type(object)</h3>
<p>返回object的类型。</p>
<p>type(name, bases, dict)</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">X</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">X</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">'X'</span><span class="p">,</span> <span class="p">(</span><span class="nb">object</span><span class="p">,),</span> <span class="nb">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># 当传入三个参数时,返回一个新的类型对象。</span>
</pre></div>
<p><span id="unichr"></span></p>
<h3>unichr(i)</h3>
<p>返回Unicode类型数据i的Unicode码。例如:<code>unichr(97)</code>返回字符串<code>u'a'</code>。</p>
<p><span id="unicode"></span></p>
<h3>unicode(object='')</h3>
<p>返回object的Unicode版字符串</p>
<p><span id="vars"></span></p>
<h3>vars([object])</h3>
<p>返回模块,类,实例或者其他任何具有__dict__属性的对象的__dict__属性。(key:value形式的__dict__属性)</p>
<p><span id="xrange"></span></p>
<h3>xrange(x)</h3>
<p>和<a href="#range">range()</a>方法一样,区别就是它返回的是一个xrange对象而不是一个列表。惰性计算!当x很大的时候,一定要用xrange。</p>
<p><span id="zip"></span></p>
<h3>zip([iterable,...])</h3>
<p>该函数返回一个元组的列表,其中第i个元素包含每个元素的序列的第i个元素。</p>
<div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="c1">#可用于转置矩阵</span>
<span class="n">zipped</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="c1">#[(1, 4), (2, 5), (3, 6)]</span>
<span class="n">x2</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">zipped</span><span class="p">)</span>
<span class="n">x</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">x2</span><span class="p">)</span> <span class="ow">and</span> <span class="n">y</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">y2</span><span class="p">)</span> <span class="c1">#True</span>
</pre></div>
<p><span id="_import_"></span></p>
<h3>__import__()</h3>
<p>这种高级引入模块的方法,不常用,所以pass</p></div>
<hr>
<div class="comments">
<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
</div>
<script type="text/javascript">
var duoshuoQuery = {short_name:"xueweihan"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = 'http://static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
<!-- Duoshuo Comment END -->
</div>
</div>
<div class="span3">
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
导航
</li>
<li><a href="http://www.xueweihan.com/archives.html">按时间</a>
<li><a href="http://www.xueweihan.com/tags.html">按标签</a>
<li><a href="http://www.xueweihan.com/feeds/all.atom.xml" rel="alternate">订阅</a></li>
</ul>
</div>
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
分类
</li>
<li><a href="http://www.xueweihan.com/category/bian-cheng.html">编程</a></li>
<li><a href="http://www.xueweihan.com/category/ri-ji.html">日记</a></li>
</ul>
</div>
<div class="social">
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
我在这里
</li>
<li><a href="https://github.com/521xueweihan">github</a></li>
<li><a href="http://www.cnblogs.com/xueweihan/">博客园</a></li>
</ul>
</div>
</div>
</div> </div> </div>
<footer>
<br />
<p><a href="http://www.xueweihan.com">削微寒</a> © xueweihan 2016</p>
</footer>
</div> <!-- /container -->
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?0d41cbe881e2dcf6beff76ecd96acf7f";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<script src="http://www.xueweihan.com/theme/jquery.min.js"></script>
<script src="http://www.xueweihan.com/theme/bootstrap-collapse.js"></script>
<script>var _gaq=[['_setAccount','UA-74442464-1'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
<a href="https://github.com/521xueweihan"><img style="position: absolute; top: 40px; right: 0; border: 0;" src="http://www.xueweihan.com/theme/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
</body>
</html>