Python基本資料型別之int

morra發表於2016-10-13

一、int的範圍

2.7:
32位:-2^31~2^31-1 64位:-2^63~2^63-1
3.5:
在3.5中init長度理論上是無限的

二、python記憶體機制

在一般情況下當變數被賦值後,記憶體和變數的關係如下:

#方式一
n1 = 123
n2 = 123

Python基本資料型別之int

#方式二
n1 = 123
n2 = n1

Python基本資料型別之int

python內的優化機制(不論是2.7還是3.5都有):
在-5~257之間的數,如果使用第一種賦值方式,那麼他們依然屬於同一塊記憶體

print(id(n1))       #檢視變數的記憶體地址

二、進位制轉換

bin()    #把變數轉換為2進位制
oct()    #把變數轉換為8進位制
int()    #把變數轉換為10進位制
hex()    #把變數轉換為16進位制

三、原始碼

class int(object):
    """
    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
    """
    def bit_length(self): ; restored from __doc__
        """
        int.bit_length() -> int
        
        Number of bits necessary to represent self in binary.
        >>> bin(37)
        '0b100101'
        >>> (37).bit_length()
        6
        """
        return 0

    def conjugate(self, *args, **kwargs): 
        """ Returns self, the complex conjugate of any int. """
        pass

    @classmethod # known case
    def from_bytes(cls, bytes, byteorder, *args, **kwargs): ; NOTE: unreliably restored from __doc__ 
        """
        int.from_bytes(bytes, byteorder, *, signed=False) -> int
        
        Return the integer represented by the given array of bytes.
        
        The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
        
        The byteorder argument determines the byte order used to represent the
        integer.  If byteorder is 'big', the most significant byte is at the
        beginning of the byte array.  If byteorder is 'little', the most
        significant byte is at the end of the byte array.  To request the native
        byte order of the host system, use `sys.byteorder' as the byte order value.
        
        The signed keyword-only argument indicates whether two's complement is
        used to represent the integer.
        """
        pass

    def to_bytes(self, length, byteorder, *args, **kwargs): ; NOTE: unreliably restored from __doc__ 
        """
        int.to_bytes(length, byteorder, *, signed=False) -> bytes
        
        Return an array of bytes representing an integer.
        
        The integer is represented using length bytes.  An OverflowError is
        raised if the integer is not representable with the given number of
        bytes.
        
        The byteorder argument determines the byte order used to represent the
        integer.  If byteorder is 'big', the most significant byte is at the
        beginning of the byte array.  If byteorder is 'little', the most
        significant byte is at the end of the byte array.  To request the native
        byte order of the host system, use `sys.byteorder' as the byte order value.
        
        The signed keyword-only argument determines whether two's complement is
        used to represent the integer.  If signed is False and a negative integer
        is given, an OverflowError is raised.
        """
        pass

    def __abs__(self, *args, **kwargs): 
        """ abs(self) """
        pass

    def __add__(self, *args, **kwargs): 
        """ Return self+value. """
        pass

    def __and__(self, *args, **kwargs): 
        """ Return self&value. """
        pass

    def __bool__(self, *args, **kwargs): 
        """ self != 0 """
        pass

    def __ceil__(self, *args, **kwargs): 
        """ Ceiling of an Integral returns itself. """
        pass

    def __divmod__(self, *args, **kwargs): 
        """ Return divmod(self, value). """
        pass

    def __eq__(self, *args, **kwargs): 
        """ Return self==value. """
        pass

    def __float__(self, *args, **kwargs): 
        """ float(self) """
        pass

    def __floordiv__(self, *args, **kwargs): 
        """ Return self//value. """
        pass

    def __floor__(self, *args, **kwargs): 
        """ Flooring an Integral returns itself. """
        pass

    def __format__(self, *args, **kwargs): 
        pass

    def __getattribute__(self, *args, **kwargs): 
        """ Return getattr(self, name). """
        pass

    def __getnewargs__(self, *args, **kwargs): 
        pass

    def __ge__(self, *args, **kwargs): 
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): 
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): 
        """ Return hash(self). """
        pass

    def __index__(self, *args, **kwargs): 
        """ Return self converted to an integer, if self is suitable for use as an index into a list. """
        pass

    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

    def __int__(self, *args, **kwargs): 
        """ int(self) """
        pass

    def __invert__(self, *args, **kwargs): 
        """ ~self """
        pass

    def __le__(self, *args, **kwargs): 
        """ Return self<=value. """
        pass

    def __lshift__(self, *args, **kwargs): 
        """ Return self<<value. """
        pass

    def __lt__(self, *args, **kwargs): 
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs): 
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs): 
        """ Return self*value. """
        pass

    def __neg__(self, *args, **kwargs): 
        """ -self """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): 
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): 
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): 
        """ Return self|value. """
        pass

    def __pos__(self, *args, **kwargs): 
        """ +self """
        pass

    def __pow__(self, *args, **kwargs): 
        """ Return pow(self, value, mod). """
        pass

    def __radd__(self, *args, **kwargs): 
        """ Return value+self. """
        pass

    def __rand__(self, *args, **kwargs): 
        """ Return value&self. """
        pass

    def __rdivmod__(self, *args, **kwargs): 
        """ Return divmod(value, self). """
        pass

    def __repr__(self, *args, **kwargs): 
        """ Return repr(self). """
        pass

    def __rfloordiv__(self, *args, **kwargs): 
        """ Return value//self. """
        pass

    def __rlshift__(self, *args, **kwargs): 
        """ Return value<<self. """
        pass

    def __rmod__(self, *args, **kwargs): 
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs): 
        """ Return value*self. """
        pass

    def __ror__(self, *args, **kwargs): 
        """ Return value|self. """
        pass

    def __round__(self, *args, **kwargs): 
        """
        Rounding an Integral returns itself.
        Rounding with an ndigits argument also returns an integer.
        """
        pass

    def __rpow__(self, *args, **kwargs): 
        """ Return pow(value, self, mod). """
        pass

    def __rrshift__(self, *args, **kwargs): 
        """ Return value>>self. """
        pass

    def __rshift__(self, *args, **kwargs): 
        """ Return self>>value. """
        pass

    def __rsub__(self, *args, **kwargs): 
        """ Return value-self. """
        pass

    def __rtruediv__(self, *args, **kwargs): 
        """ Return value/self. """
        pass

    def __rxor__(self, *args, **kwargs): 
        """ Return value^self. """
        pass

    def __sizeof__(self, *args, **kwargs): 
        """ Returns size in memory, in bytes """
        pass

    def __str__(self, *args, **kwargs): 
        """ Return str(self). """
        pass

    def __sub__(self, *args, **kwargs): 
        """ Return self-value. """
        pass

    def __truediv__(self, *args, **kwargs): 
        """ Return self/value. """
        pass

    def __trunc__(self, *args, **kwargs): 
        """ Truncating an Integral returns itself. """
        pass

    def __xor__(self, *args, **kwargs): 
        """ Return self^value. """
        pass

    denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the denominator of a rational number in lowest terms"""

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the imaginary part of a complex number"""

    numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the numerator of a rational number in lowest terms"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the real part of a complex number"""

相關文章