对于python,一切事物都是对象,对象基于类创建。
例子:
li=[11,22,33]
li.append(44)
对象所具备的方法,例如:li列表的增删改查,都不是对象自己所有的,它只是去到这个类里面的方法执行的!
二.数据内型的内置方法
type() 查看对象的类型
dir(类型名) 查看类中提供的所有功能
help(类型名) 查看类中所有详细的功能
help(类型名.功能名) 查看类中某功能的详细
关于dir(),之后,你会看到两边带下划线和不带下划线的方法:
__方法__ 内置方法,可能有多种执行方法,至少一种
方法 非内置方法,只有一种执行方法,对象.方法
int例子:
(1)dir 查看所有属性
>>> 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__
(2)__float__
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'> #转换为浮点型
(3)__floordiv__
##地板除
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
(4)__pos__
def __pos__(self):
""" x.__pos__() <==> +x """
pass
>>> 2*8
16
>>> 2**8
256
>>> n = 2
>>> n.__pow__(8)
256
(5)__index__
def __index__(self):
""" 用于切片,数字无意义 """
""" x[y:z] <==> x[y.__index__():z.__index__()] """
pass
#为什么一个对象能切片啊,因为里面有index方法!
(6)__cmp__
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
(7)__divmod__
##需求:
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) (商,余数)
#余数为0,表示刚好分页
#余数为1,表示商还要+1,才够分页
(8)__radd__
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
(9)__hash__
def __hash__(self):
"""如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
""" x.__hash__() <==> hash(x) """
pass
>>> a="dfdsaufsuhadhfsaduhfsadfsdfdfsdafhsduifhsuidhfusdhfhsdufhsdfuhsdifisdjfiosdjfsdjfdsfdjds"
>>> a.__hash__()
5423373101089537918
(10)__hex__
def __hex__(self):
""" 返回当前数的 十六进制 表示 """
""" x.__hex__() <==> hex(x) """
pass
>>> age
18
>>> age.__hex__()
'0x12'
(11)__oct__
def __oct__(self):
""" 返回改值的 八进制 表示 """
""" x.__oct__() <==> oct(x) """
pass
>>> age
18
>>> age.__oct__()
'022'
(12)__neg__
def __neg__(self):
""" x.__neg__() <==> -x """
pass
>>> age
18
>>> age.__neg__()
-18
end