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,如需转载请自行联系原作者