天天看點

Python内置函數

Built-in Functions

<a href="https://docs.python.org/2/library/functions.html#abs" target="_blank">abs()</a>

<a href="https://docs.python.org/2/library/functions.html#divmod" target="_blank">divmod()</a>

<a href="https://docs.python.org/2/library/functions.html#input" target="_blank">input()</a>

<a href="https://docs.python.org/2/library/functions.html#open" target="_blank">open()</a>

<a href="https://docs.python.org/2/library/functions.html#staticmethod" target="_blank">staticmethod()</a>

<a href="https://docs.python.org/2/library/functions.html#all" target="_blank">all()</a>

<a href="https://docs.python.org/2/library/functions.html#enumerate" target="_blank">enumerate()</a>

<a href="https://docs.python.org/2/library/functions.html#int" target="_blank">int()</a>

<a href="https://docs.python.org/2/library/functions.html#ord" target="_blank">ord()</a>

<a href="https://docs.python.org/2/library/functions.html#str" target="_blank">str()</a>

<a href="https://docs.python.org/2/library/functions.html#any" target="_blank">any()</a>

<a href="https://docs.python.org/2/library/functions.html#eval" target="_blank">eval()</a>

<a href="https://docs.python.org/2/library/functions.html#isinstance" target="_blank">isinstance()</a>

<a href="https://docs.python.org/2/library/functions.html#pow" target="_blank">pow()</a>

<a href="https://docs.python.org/2/library/functions.html#sum" target="_blank">sum()</a>

<a href="https://docs.python.org/2/library/functions.html#basestring" target="_blank">basestring()</a>

<a href="https://docs.python.org/2/library/functions.html#execfile" target="_blank">execfile()</a>

<a href="https://docs.python.org/2/library/functions.html#issubclass" target="_blank">issubclass()</a>

<a href="https://docs.python.org/2/library/functions.html#print" target="_blank">print()</a>

<a href="https://docs.python.org/2/library/functions.html#super" target="_blank">super()</a>

<a href="https://docs.python.org/2/library/functions.html#bin" target="_blank">bin()</a>

<a href="https://docs.python.org/2/library/functions.html#file" target="_blank">file()</a>

<a href="https://docs.python.org/2/library/functions.html#iter" target="_blank">iter()</a>

<a href="https://docs.python.org/2/library/functions.html#property" target="_blank">property()</a>

<a href="https://docs.python.org/2/library/functions.html#tuple" target="_blank">tuple()</a>

<a href="https://docs.python.org/2/library/functions.html#bool" target="_blank">bool()</a>

<a href="https://docs.python.org/2/library/functions.html#filter" target="_blank">filter()</a>

<a href="https://docs.python.org/2/library/functions.html#len" target="_blank">len()</a>

<a href="https://docs.python.org/2/library/functions.html#range" target="_blank">range()</a>

<a href="https://docs.python.org/2/library/functions.html#type" target="_blank">type()</a>

<a href="https://docs.python.org/2/library/functions.html#bytearray" target="_blank">bytearray()</a>

<a href="https://docs.python.org/2/library/functions.html#float" target="_blank">float()</a>

<a href="https://docs.python.org/2/library/functions.html#list" target="_blank">list()</a>

<a href="https://docs.python.org/2/library/functions.html#raw_input" target="_blank">raw_input()</a>

<a href="https://docs.python.org/2/library/functions.html#unichr" target="_blank">unichr()</a>

<a href="https://docs.python.org/2/library/functions.html#callable" target="_blank">callable()</a>

<a href="https://docs.python.org/2/library/functions.html#format" target="_blank">format()</a>

<a href="https://docs.python.org/2/library/functions.html#locals" target="_blank">locals()</a>

<a href="https://docs.python.org/2/library/functions.html#reduce" target="_blank">reduce()</a>

<a href="https://docs.python.org/2/library/functions.html#unicode" target="_blank">unicode()</a>

<a href="https://docs.python.org/2/library/functions.html#chr" target="_blank">chr()</a>

<a href="https://docs.python.org/2/library/functions.html#func-frozenset" target="_blank">frozenset()</a>

<a href="https://docs.python.org/2/library/functions.html#long" target="_blank">long()</a>

<a href="https://docs.python.org/2/library/functions.html#reload" target="_blank">reload()</a>

<a href="https://docs.python.org/2/library/functions.html#vars" target="_blank">vars()</a>

<a href="https://docs.python.org/2/library/functions.html#classmethod" target="_blank">classmethod()</a>

<a href="https://docs.python.org/2/library/functions.html#getattr" target="_blank">getattr()</a>

<a href="https://docs.python.org/2/library/functions.html#map" target="_blank">map()</a>

<a href="https://docs.python.org/2/library/functions.html#func-repr" target="_blank">repr()</a>

<a href="https://docs.python.org/2/library/functions.html#xrange" target="_blank">xrange()</a>

<a href="https://docs.python.org/2/library/functions.html#cmp" target="_blank">cmp()</a>

<a href="https://docs.python.org/2/library/functions.html#globals" target="_blank">globals()</a>

<a href="https://docs.python.org/2/library/functions.html#max" target="_blank">max()</a>

<a href="https://docs.python.org/2/library/functions.html#reversed" target="_blank">reversed()</a>

<a href="https://docs.python.org/2/library/functions.html#zip" target="_blank">zip()</a>

<a href="https://docs.python.org/2/library/functions.html#compile" target="_blank">compile()</a>

<a href="https://docs.python.org/2/library/functions.html#hasattr" target="_blank">hasattr()</a>

<a href="https://docs.python.org/2/library/functions.html#func-memoryview" target="_blank">memoryview()</a>

<a href="https://docs.python.org/2/library/functions.html#round" target="_blank">round()</a>

<a href="https://docs.python.org/2/library/functions.html#__import__" target="_blank">__import__()</a>

<a href="https://docs.python.org/2/library/functions.html#complex" target="_blank">complex()</a>

<a href="https://docs.python.org/2/library/functions.html#hash" target="_blank">hash()</a>

<a href="https://docs.python.org/2/library/functions.html#min" target="_blank">min()</a>

<a href="https://docs.python.org/2/library/functions.html#func-set" target="_blank">set()</a>

<a href="https://docs.python.org/2/library/functions.html#apply" target="_blank">apply()</a>

<a href="https://docs.python.org/2/library/functions.html#delattr" target="_blank">delattr()</a>

<a href="https://docs.python.org/2/library/functions.html#help" target="_blank">help()</a>

<a href="https://docs.python.org/2/library/functions.html#next" target="_blank">next()</a>

<a href="https://docs.python.org/2/library/functions.html#setattr" target="_blank">setattr()</a>

<a href="https://docs.python.org/2/library/functions.html#buffer" target="_blank">buffer()</a>

<a href="https://docs.python.org/2/library/functions.html#func-dict" target="_blank">dict()</a>

<a href="https://docs.python.org/2/library/functions.html#hex" target="_blank">hex()</a>

<a href="https://docs.python.org/2/library/functions.html#object" target="_blank">object()</a>

<a href="https://docs.python.org/2/library/functions.html#slice" target="_blank">slice()</a>

<a href="https://docs.python.org/2/library/functions.html#coerce" target="_blank">coerce()</a>

<a href="https://docs.python.org/2/library/functions.html#dir" target="_blank">dir()</a>

<a href="https://docs.python.org/2/library/functions.html#id" target="_blank">id()</a>

<a href="https://docs.python.org/2/library/functions.html#oct" target="_blank">oct()</a>

<a href="https://docs.python.org/2/library/functions.html#sorted" target="_blank">sorted()</a>

<a href="https://docs.python.org/2/library/functions.html#intern" target="_blank">intern()</a>

内置方法

 說明

 __init__(self,...)

 初始化對象,在建立新對象時調用

 __del__(self)

 釋放對象,在對象被删除之前調用

 __new__(cls,*args,**kwd)

 執行個體的生成操作

 __str__(self)

 在使用print語句時被調用

 __getitem__(self,key)

 擷取序列的索引key對應的值,等價于seq[key]

 __len__(self)

 在調用内聯函數len()時被調用

 __cmp__(stc,dst)

 比較兩個對象src和dst

 __getattr__(s,name)

 擷取屬性的值

 __setattr__(s,name,value)

 設定屬性的值

 __delattr__(s,name)

 删除name屬性

 __getattribute__()

 __getattribute__()功能與__getattr__()類似

 __gt__(self,other)

 判斷self對象是否大于other對象

 __lt__(slef,other)

 判斷self對象是否小于other對象

 __ge__(slef,other)

 判斷self對象是否大于或者等于other對象

 __le__(slef,other)

 判斷self對象是否小于或者等于other對象

 __eq__(slef,other)

 判斷self對象是否等于other對象

 __call__(self,*args)

 把執行個體對象作為函數調用

__init__方法在類的一個對象被建立時,馬上運作。這個方法可以用來對你的對象做一些你希望的初始化。注意,這個名稱的開始和結尾都是雙下劃線。

代碼例子:

說明:__init__方法定義為取一個參數name(以及普通的參數self)。在這個__init__裡,我們隻是建立一個新的域,也稱為name。注意它們是兩個不同的變量,盡管它們有相同的名字。點号使我們能夠區分它們。最重要的是,我們沒有專門調用__init__方法,隻是在建立一個類的新執行個體的時候,把參數包括在圓括号内跟在類名後面,進而傳遞給__init__方法。這是這種方法的重要之處。現在,我們能夠在我們的方法中使用self.name域。這在sayHi方法中得到了驗證。

__new__()在__init__()之前被調用,用于生成執行個體對象.利用這個方法和類屬性的特性可以實作設計模式中的單例模式.單例模式是指建立唯一對象嗎,單例模式設計的類隻能執行個體化一個對象.

當讀取對象的某個屬性時,python會自動調用__getattr__()方法.例如,fruit.color将轉換為fruit.__getattr__(color).當使用指派語句對屬性進行設定時,python會自動調用__setattr__()方法.__getattribute__()的功能與__getattr__()類似,用于擷取屬性的值.但是__getattribute__()能提供更好的控制,代碼更健壯.注意,python中并不存在__setattribute__()方法.

代碼例子:

<code>#!/usr/bin/python</code>

<code># -*- coding: UTF-8 -*-</code>

<code>class</code> <code>Fruit(</code><code>object</code><code>):</code>

<code>    </code><code>def</code> <code>__init__(</code><code>self</code><code>, color </code><code>=</code> <code>"red"</code><code>, price </code><code>=</code> <code>0</code><code>):</code>

<code>       </code><code>self</code><code>.__color </code><code>=</code> <code>color</code>

<code>       </code><code>self</code><code>.__price </code><code>=</code> <code>price</code>

<code>        </code> 

<code>    </code><code>def</code> <code>__getattribute__(</code><code>self</code><code>, name):               </code><code># 擷取屬性的方法</code>

<code>       </code><code>return</code> <code>object</code><code>.__getattribute__(</code><code>self</code><code>, name)</code>

<code>    </code><code>def</code> <code>__setattr__(</code><code>self</code><code>, name, value):</code>

<code>       </code><code>self</code><code>.__dict__[name] </code><code>=</code> <code>value</code>

<code>if</code> <code>__name__ </code><code>=</code><code>=</code> <code>"__main__"</code><code>:</code>

<code>   </code><code>fruit </code><code>=</code> <code>Fruit(</code><code>"blue"</code><code>, </code><code>10</code><code>)</code>

<code>   </code><code>print</code> <code>fruit.__dict__.get(</code><code>"_Fruit__color"</code><code>)       </code><code># 擷取color屬性</code>

<code>   </code><code>fruit.__dict__[</code><code>"_Fruit__price"</code><code>] </code><code>=</code> <code>5</code>

<code>   </code><code>print</code> <code>fruit.__dict__.get(</code><code>"_Fruit__price"</code><code>)       </code><code># 擷取price屬性</code>

如果類把某個屬性定義為序列,可以使用__getitem__()輸出序列屬性中的某個元素.假設水果店中銷售多鐘水果,可以通過__getitem__()方法擷取水果店中的沒種水果

#!/usr/bin/python

# -*- coding: UTF-8 -*-

class FruitShop:

      def __getitem__(self, i):      # 擷取水果店的水果

            return self.fruits[i]      

if __name__ == "__main__":

     shop = FruitShop()

     shop.fruits = ["apple", "banana"]

     print shop[1]

     for item in shop:               # 輸出水果店的水果

          print item,

輸出為:

banana

apple banana

<code>輸出為:</code>

<code>banana</code>

<code>apple banana</code>

__str__()用于表示對象代表的含義,傳回一個字元串.實作了__str__()方法後,可以直接使用print語句輸出對象,也可以通過函數str()觸發__str__()的執行.這樣就把對象和字元串關聯起來,便于某些程式的實作,可以用這個字元串來表示某個類

在類中實作__call__()方法,可以在對象建立時直接傳回__call__()的内容.使用該方法可以模拟靜态方法

    Fruit.grow()         # 輸出結果:grow ...

本文轉自 奚落123 51CTO部落格,原文連結:http://blog.51cto.com/guyuyuan/1920932,如需轉載請自行聯系原作者