Python 整数 长整数 浮点数 字符串 列表 元组 字典的各种方法

对于Python, 一切事物都是对象,对象基于类创建!!

注:查看对象相关成员var,type, dir

一、整数

如: 18、73、84

每一个整数都具备如下需要知道的功能:

def bit_length(self): 
        """ 返回表示该数字的时占用的最少位数 """
        """ int.bit_length() -> int Number of bits necessary to represent self in binary. >>> bin(37) '0b100101' >>> (37).bit_length() """ return 0
age = 18 
age.bit_length() --> 5
def conjugate(self, *args, **kwargs): # real signature unknown
        """ 返回该复数的共轭复数 """
        """ Returns self, the complex conjugate of any int. """
        pass
#了解
def __abs__(self):
        """ 返回绝对值 """
        """ x.__abs__() <==> abs(x) """
        pass
age = -18
age.__abs__() --> 18
print(abs(age)) --> 18

 __add__ , __and__, __cmp__都是这样的用法

重点说一下,__divmod__()

def __divmod__(self, y): 
        """ 相除,得到商和余数组成的元组 """ 
        """ x.__divmod__(y) <==> divmod(x, y) """
        pass
age=18
age.__divmod__(2) --> (9,0)
divmod(18,2) --> (9, 0)

 __hex__(), __int__(), __float__(), __hex__(), __long__()这些方法都有默认的自带函数方法如int(),float(), long()等。。。

猜你喜欢

转载自www.cnblogs.com/jpinsz/p/9822259.html