一.python基础
>>> dir(int)
[‘__abs__‘,‘__add__‘,‘__and__‘,‘__class__‘,‘__cmp__‘,‘__coerce__‘,‘__delattr__‘,‘__div__‘,‘__divmod__‘,‘__doc__‘,‘__float__‘,‘__floordiv__‘,‘__format__‘,‘__getattribute__‘,‘__getnewargs__‘,‘__hash__‘,‘__hex__‘,‘__index__‘,‘__init__‘,‘__int__‘,‘__invert__‘,‘__long__‘,‘__lshift__‘,‘__mod__‘,‘__mul__‘,‘__neg__‘,‘__new__‘,‘__nonzero__‘,‘__oct__‘,‘__or__‘,‘__pos__‘,‘__pow__‘,‘__radd__‘,‘__rand__‘,‘__rdiv__‘,‘__rdivmod__‘,‘__reduce__‘,‘__reduce_ex__‘,‘__repr__‘,‘__rfloordiv__‘,‘__rlshift__‘,‘__rmod__‘,‘__rmul__‘,‘__ror__‘,‘__rpow__‘,‘__rrshift__‘,‘__rshift__‘,‘__rsub__‘,‘__rtruediv__‘,‘__rxor__‘,‘__setattr__‘,‘__sizeof__‘,‘__str__‘,‘__sub__‘,‘__subclasshook__‘,‘__truediv__‘,‘__trunc__‘,‘__xor__‘,‘bit_length‘,‘conjugate‘,‘denominator‘,‘imag‘,‘numerator‘,‘real‘]
>>> n1 =1
>>> n2 =2
>>> n1 + n2
3
>>> n1.__add__(n2)
3
#经过python的解析器解析,+号其实最后会转换为内置方法__add__
help(int)
....
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
....
>>> int("11",base=2)#base表示要转换为2进制
3
>>> int("F",base=16)#base表示要转换为16进制
15
>>> age=19
>>> age.__cmp__(3)#19比3大,返回1
1
>>> age.__cmp__(32)#19比32小,返回-1
-1
>>> age.__cmp__(19)#两个19比较,返回0
0
>>> cmp(19,3)#19比3大,返回1
1
>>> cmp(19,32)#19比32小,返回-1
-1
>>> cmp(19,19)#两个19比较,返回0
0
##需求:
99条数据
每个页面10条数据
?多少个页面
def __divmod__(self, y):
""" 相除,得到商和余数组成的元组 """
""" x.__divmod__(y) <==> divmod(x, y) """
pass
>>> a =99
>>> a.__divmod__(10)
(9,9) (商,余数)
>>> a=98
>>> a.__divmod__(10)
(9,8)
(商,余数)def __float__(self):
""" 转换为浮点类型 """
""" x.__float__() <==> float(x) """
pass
>>> age=18
>>> type(age)#整形
<type ‘int‘>
>>> age.__float__()
18.0
>>> nage=age.__float__()
>>> type(nage)
<type ‘float‘>#转换为浮点型
##地板除
def __floordiv__(self, y):
""" x.__floordiv__(y) <==> x//y """
pass
>>>5/2
2
>>>5//2
2
>>>5.0/2
2.5
>>>5.0//2#不保留小数后面的
2.0
def __hash__(self):
"""如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
""" x.__hash__() <==> hash(x) """
pass
>>> a="dfdsaufsuhadhfsaduhfsadfsdfdfsdafhsduifhsuidhfusdhfhsdufhsdfuhsdifisdjfiosdjfsdjfdsfdjds"
>>> a.__hash__()
5423373101089537918
def __hex__(self):
""" 返回当前数的 十六进制 表示 """
""" x.__hex__() <==> hex(x) """
pass
>>> age
18
>>> age.__hex__()
‘0x12‘
def __oct__(self):
""" 返回改值的 八进制 表示 """
""" x.__oct__() <==> oct(x) """
pass
>>> age
18
>>> age.__oct__()
‘022‘
def __neg__(self):
""" x.__neg__() <==> -x """
pass
>>> age
18
>>> age.__neg__()
-18
def __pos__(self):
""" x.__pos__() <==> +x """
pass
>>>2*8
16
>>>2**8
256
>>> n =2
>>> n.__pow__(8)
256
def __radd__(self, y):
""" x.__radd__(y) <==> y+x """
pass
>>> n
2
>>> n.__add__(3)#2+3
5
>>> n+3
5
>>> n+5
7
>>> n.__radd__(3)#3+2
5
>>> n.__radd__(5)#3+5
7
def __index__(self):
""" 用于切片,数字无意义 """
""" x[y:z] <==> x[y.__index__():z.__index__()] """
pass
#为什么一个对象能切片啊,因为里面有index方法!
原文:http://www.cnblogs.com/binhy0428/p/5125938.html