类与对象
一、内置函数
1、int方法
(1) def __add__ (self, y): #两数相加
""" x.__add__(y) <==> x+y """
例:
1 #!/usr/bin/env python2 x=83 result=x.__add__(9)4 print(result) 输出:17
(2)def __divmod__ # 取模 ,应 场 用景:页面分页 __rdivmod__(反向运算)
例:
1 #!/usr/bin/env python2 age=183 result=age.__divmod__(2)4 print(result)输出:(9,0)
(3)def __abs__(绝对值)
例:
1 #!/usr/bin/env python2 x=-183 result=x.__abs__()4 print(result) 输出:18
(4)__pow__(幂)
def __pow__(self, y, z=None): """ 幂,次方 """""" x.__pow__(y[, z]) <==> pow(x, y[, z]) """例:
1 #!/usr/bin/env python2 x=23 result=x.__pow__(4)4 print(result) 输出:16
(5)__ge__(与自身比较大小,返回真假)
例:
1 #!/usr/bin/env python2 x=23 result=x.__ge__(5)4 print(result) 输出:false
(6) def __and__ (self, y):(逻辑运算“和”,相当于 &)
""" x.__and__(y) <==> x&y """
例:
1 #!/usr/bin/env python2 x=23 result=x.__and__(8)4 print(result) 输出:0
(7)def__coerce__(self, y):
""" 强制生成一个元组 """""" x.__coerce__(y) <==> coerce(x, y) """
(8)def__div__(self, y): """ x.__div__(y) <==> x/y """
例:
1 #!/usr/bin/env python2 x =123 result=x.__div__(2)4 print(result) 输出:6
(9) def_ _float__ (self):
""" 转换为浮点类型 """""" x.__float__() <==> float(x) """
例:
1 #!/usr/bin/env python2 x =123 result=x.__float__()4 print(result)5 print(type(result)) 输出:12.0 class 'float'
(10) def __floordiv__ (self, y): (地板除)
""" x.__floordiv__(y) <==> x//y """
例:
1 #!/usr/bin/env python2 x =123 result=x.__floordiv__(2)4 print(result) 输出:6
(11) def __hex__ (self):
""" 返回当前数的 十六进制 表示 """""" x.__hex__() <==> hex(x) """
(12)def__index__(self):
""" 用于切片,数字无意义 """""" x[y:z] <==> x[y.__index__():z.__index__()] """
(13)def__int__(self):
""" 转换为整数 """""" x.__int__() <==> int(x) """
例:
1 #!/usr/bin/env python2 x =5.33 result=x.__int__()4 print(result) 输出:5
( 14) def __long__ (self):
""" 转换为长整数 """""" x.__long__() <==> long(x) """
(15)def__mod__(self, y): #求余数
""" x.__mod__(y) <==> x%y """
例:
1 #!/usr/bin/env python2 x =93 result=x.__mod__(2)4 print(result) 输出:1
(16)def__mul__(self, y): #乘法运算
""" x.__mul__(y) <==> x*y """
例:
1 #!/usr/bin/env python2 x =93 result=x.__mul__(2)4 print(result) 输出:18
(17) def __neg__ (self): #取反
""" x.__neg__() <==> -x """ 例:
1 #!/usr/bin/env python2 x =93 result=x.__neg__()4 print(result)
(18) def __nonzero__ (self): #不为0
""" x.__nonzero__() <==> x != 0 """
2、长整型long与浮点型flot长整型、浮点型各种方法基本与整型int保持一致,不在一一列举赘述
3、字符串str方法
(1)print(type(name)) #type获取属于哪个类
print(dir(name)) #dir获取类的方法
(2) def __contains__(self, y):
""" x.__contains__(y) <==> y in x """ 例:
1 #!/usr/bin/env python2 name='alvin'3 result=name.__contains__('oo')#contains是否包含给出字符4 print(result)
(3)name.capitalize() #首字母大写
例:
1 #!/usr/bin/env python2 x ='alvin'3 result=x.capitalize()4 print(result) 输出:Alvin
(4)name.endswith('n',0,3) # 判断是否以n结尾的,True and False
例:
1 #!/usr/bin/env python2 name = str('alvin')3 result=name.endswith('n')4 print(result) 输出:True
(5)name.center(20,‘*’) # 字符串居中,以*分割
例:
1 #!/usr/bin/env python2 x ='alvin'3 result=x.center(20,'*')4 print(result)
(6)name.endcode() #转变字符串的编码
(7)count() #统计子序列出现的次数
例:
1 #!/usr/bin/env python2 name='alvin'3 result=name.count('n',0,4)#从0开始至第四个字符‘n’出现的个数4 print(result)
(9)find('al') #查找是否包含子序列'al' index:找不到会报错
(10)format() #字符串的拼接 例:
1 #!/usr/bin/env python2 name ='alvin is {0} as {1}'3 result = name.format('smart','hello')#result=name.format(0='smart',1='hello')4 print(result)