int内部方法释义

python基本数据类型包括:int、str、list、tuple、dict、bool、set(),一切事物都是对象,对象由类创建

1. bit_length:返回数字占用的二进制最小位数

def bit_length(self): # real signature unknown; restored from __doc__
        """
        int.bit_length() -> int
        
        Number of bits necessary to represent self in binary.
        >>> bin(37)
        '0b100101'
        >>> (37).bit_length()
        """
        return 0
eg.
>>> a=25
>>> bin(25)
>>> '0b11001'
>>> a.bit_length()
>>> 5

2. conjugate:返回复数的共轭复数

a+bi(a,b均为实数)的数称为复数,其中a称为实部,b称为虚部,i称为虚数单位

def conjugate(self, *args, **kwargs): # real signature unknown
        """ Returns self, the complex conjugate of any int. """
        pass
eg.
>>> num=1.2+2.3j
>>> num.real  
>>> 1.2                  
>>> num.imag 
>>> 2.3
>>> num.conjugate()   
>>> 1.2-2.3j   

 3. __abs__:返回绝对值

def __abs__(self, *args, **kwargs): # real signature unknown
        """ abs(self) """
        pass
eg.
>>> num=-10
>>> num.__abs__()
>>> 10

 4. __neg__:返回负值

def __neg__(self, *args, **kwargs): # real signature unknown
        """ -self """
        pass
eg.
>>> a=5
>>> a.__neg__()
>>> -5

 5. __add__:返回二者之和(self+value)

def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass
eg.
>>> num=-10
>>> num.__add__(15)
>>> 5

 6. __radd__:返回二者之和(self+value

def __radd__(self, *args, **kwargs): # real signature unknown
        """ Return value+self. """
        pass
eg.
>>> a=5
>>> a.__radd__(10)
>>> 15

 7.___and__:按位与运算(self&value) 

def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass
eg.
>>> num1=25                      #--->0b11001
>>> num2=18                      #--->0b10010
>>> num1.__and__(num2)           # num1&num2
>>> 16                           #--->0b10000

 8. __rand__:按位与运算(value&self)

def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass
eg.
>>> a=3
>>> a.__rand__(5)
>>> 1
# 3 转换成二进制:011,5转换成二进制:101,3&5--->001

9. __or__:按位或运算(self|value

def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass
eg. 
>>> a=10
>>> a.__or__(6)
>>> 14
#10转换成二进制:1010,6转换成二进制0110,10|6-->1110

10. __ror__:按位或运算(value|self)

def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass
eg.
>>> a=5
>>> a.__ror__(10)
>>> 15
# 5-->0101,10-->1010,10|5-->1111

11. __invert__:按位翻转运算,返回值为-(x+1)

def __invert__(self, *args, **kwargs): # real signature unknown
        """ ~self """
        pass
eg.
>>> a=15
>>> a.__invert__()
>>> -16

12. __lshift__:按位左移运算,self<<value

 def __lshift__(self, *args, **kwargs): # real signature unknown
        """ Return self<<value. """
        pass
eg.
>>> a=10
>>> a.__lshift__(2)   # a左移2位
>>> 40 

# 10 转换成二进制为:00001010,左移2位为:00010100

13. __rlshift__:按位左移运算,value<<self

 def __rlshift__(self, *args, **kwargs): # real signature unknown
        """ Return value<<self. """
        pass
eg.
>>> a=2
>>> a.__rlshift__(10)
>>> 40
#10转换成二进制:1010,左移2位后为:101000

14. __rshift__:按位右移运算,self>>value

def __rshift__(self, *args, **kwargs): # real signature unknown
        """ Return self>>value. """
        pass
eg.
>>> a=10
>>> a.__rshift__(2)
>>> 2
#10-->1010 ,右移2位-->0010

15. __rrshift__:按位右移运算,value>>self

def __rrshift__(self, *args, **kwargs): # real signature unknown
        """ Return value>>self. """
        pass
eg.
>>> a=1
>>> a.__rrshift__(10)
>>> 5
# 10 -->1010 ,右移1位-->0101

16.__xor__:按位异或(self^value)

def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass
eg.
>>> a=10
>>> a.__xor__(5)
>>> 15
#10-->1010,5-->0101,10^5-->1111

17. __rxor__:按位异或(value^self)

def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass
eg.
>>> a=2
>>> a.__rxor__(5)
>>> 7
#010^101=111

18. __bool_:返回bool值,即True  or  False

def __bool__(self, *args, **kwargs): # real signature unknown
        """ self != 0 """
        pass
eg.
>>> a=10
>>> a.__bool__()
>>> True

19. __divmod__:返回商和余数(self/value)

def __divmod__(self, *args, **kwargs): # real signature unknown
        """ Return divmod(self, value). """
        pass
eg.
>>> a=13
>>> b=4
>>> a.__divmod__(b)
>>> (3,1)

20. __rdivmod__:返回商和余数(value/self

def __rdivmod__(self, *args, **kwargs): # real signature unknown
        """ Return divmod(value, self). """
        pass
eg.
>>> a=10
>>> a.__rdivmod__(25)
>>> (2,5)

21. __mod__:返回余数(self%value

def __mod__(self, *args, **kwargs): # real signature unknown
        """ Return self%value. """
        pass
eg. 
>>> a=5
>>> a.__mod__(2)
>>> 1

22. __rmod__:返回余数(value%self)

def __rmod__(self, *args, **kwargs): # real signature unknown
        """ Return value%self. """
        pass
eg.
>>> a=10
>>> a.__rmod__(25)
>>> 5

#25%10

23. __floordiv__:返回商(self//value)

def __floordiv__(self, *args, **kwargs): # real signature unknown
        """ Return self//value. """
        pass
eg.
>>> a=10
>>> b=4
>>> a.__floordiv__(b)
>>> 2

24. __rfloordiv__:返回商(value//self)

 def __rfloordiv__(self, *args, **kwargs): # real signature unknown
        """ Return value//self. """
        pass
eg.
>>> a=10
>>> a.__rfloordiv__(25)
>>> 2
# 25//10

25. __truediv__:返回商(self/value)

  def __truediv__(self, *args, **kwargs): # real signature unknown
        """ Return self/value. """
        pass
eg.
>>> a=10
>>> a.__truediv__(5)
>>> 2.0

26. __rtruediv__:返回商(value/self)

def __rtruediv__(self, *args, **kwargs): # real signature unknown
        """ Return value/self. """
        pass
eg.
>>> a=10
>>> a.__rtruediv__(20)
>>> 2.0

27. __mul__:返回乘积(self*value)

def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass
eg.
>>> a=5
>>> a.__mul__(3)
>>> 15

28. __rmul__:返回乘积(value*self

def __rmul__(self, *arg s, **kwargs): # real signature unknown
        """ Return value*self. """
        pass
eg.
>>> a=5
>>> a.__rmul__(10)
>>> 50
#10*5

29. __pow__:求幂(self**value)

def __pow__(self, *args, **kwargs): # real signature unknown
        """ Return pow(self, value, mod). """
        pass
eg.
>>> a=5
>>> a.__pow__(2)
>>> 25

30. __rpow__:求幂(value**self)

def __rpow__(self, *args, **kwargs): # real signature unknown
        """ Return pow(value, self, mod). """
        pass
eg.
>>> a=2
>>> a.__rpow__(5)
>>> 25
# 5**2

31. __sub__:返回差(self-value)

 def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass
eg.
>>> a=10
>>> a.__sub__(4)
>>> 6

32.__rsub__:返回差(value-self)

def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass
eg.
>>> a=5
>>> a.__rsub__(12)
>>> 7

33. __float__:转换成浮点类型 

def __float__(self, *args, **kwargs): # real signature unknown
        """ float(self) """
        pass

eg.
>>> a=10
>>> a.__float__()
>>> 10.0

34. __int__:转换成整型

def __int__(self, *args, **kwargs): # real signature unknown
        """ int(self) """
        pass

35. __str__:转换成str类型

def __str__(self, *args, **kwargs): # real signature unknown
        """ Return str(self). """
        pass
eg.
>>> a=2
>>> a.__str__()
>>> '2'

36. __init__:int构造方法,操作a=10 or  a=int(10)自动调用

 def __init__(self, x, base=10): # known special case of int.__init__
        """
        int(x=0) -> integer
        int(x, base=10) -> integer
        
        Convert a number or string to an integer, or return 0 if no arguments
        are given.  If x is a number, return x.__int__().  For floating point
        numbers, this truncates towards zero.
        
        If x is not a number or if base is given, then x must be a string,
        bytes, or bytearray instance representing an integer literal in the
        given base.  The literal can be preceded by '+' or '-' and be surrounded
        by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
        Base 0 means to interpret the base from the string as an integer literal.
        >>> int('0b100', base=0)
        4
        # (copied from class doc)
        """
        pass

37. __eq__:比较二者是否相等,相等则返回True,否则返回False

def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass
eg.
>>> a=10
>>> b=10
>>> c=11
>>> a.__eq__(b)
>>> True
>>> a.__eq__(c)
>>> False

38. __ne__:判断self!=value,则返回True,否则返回False

 def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass
eg.
>>> a=5
>>> a.__ne__(6)
>>> True
>>> a.__ne__(5)
>>> False

 39. __ge__:比较self与values值,若self>=value,则返回True

def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass
eg.
>>> a=10
>>> b=5
>>> a.__ge__(b)
>>> True

40. __gt__:比较self与values值,若self>value,则返回True

def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass
eg.
>>> a=10
>>> a.__gt__(5)
>>> True
>>> a.__gt__(11)
>>> False

41. __le__:比较self与value值,若self<=value,则返回True

def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass
eg.
>>> a=5
>>> a.__le__(5)
>>> True
>>> a.__le__(4)
>>> False
>>> a.__le__(6)
>>> True

42. __lt__:比较self与value值,若self<value,则返回True

def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass
eg.
>>> a=5
>>> a.__lt__(5)
>>> False
>>> a.__lt__(4)
>>> False
>>> a.__lt__(6)
>>> True

43. __hash__:如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等,等于hash(values)

def __hash__(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        pass

44. __sizeof__:返回占用内存的字节数

 def __sizeof__(self, *args, **kwargs): # real signature unknown
        """ Returns size in memory, in bytes """
        pass
eg.
>>> a=2
>>> a.__sizeof__()
>>> 28

 

posted on 2018-01-17 16:34  柠檬加醋  阅读(458)  评论(0编辑  收藏  举报

导航