對象,常見數據類型與序列的內部功能,collections模塊 ...
摘要:
對象
對於python來說,一切事物都是對象,對象基於類創建:
註:查看對象相關成員 var,type,dir
基本數據類型和序列
int內部功能
1 class int(object): 2 """ 3 int(x=0) -> integer 4 int(x, base=10) -> integer 5 6 Convert a number or string to an integer, or return 0 if no arguments 7 are given. If x is a number, return x.__int__(). For floating point 8 numbers, this truncates towards zero. 9 10 If x is not a number or if base is given, then x must be a string, 11 bytes, or bytearray instance representing an integer literal in the 12 given base. The literal can be preceded by '+' or '-' and be surrounded 13 by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. 14 Base 0 means to interpret the base from the string as an integer literal. 15 >>> int('0b100', base=0) 16 4 17 """ 18 def bit_length(self): # real signature unknown; restored from __doc__ 19 #返回該數字最少二進位位數 20 """ 21 int.bit_length() -> int 22 23 Number of bits necessary to represent self in binary. 24 >>> bin(37) 25 '0b100101' 26 >>> (37).bit_length() 27 6 28 """ 29 return 0 30 31 def conjugate(self, *args, **kwargs): # real signature unknown 32 """ Returns self, the complex conjugate of any int. """ 33 pass 34 35 @classmethod # known case 36 def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 37 """ 38 int.from_bytes(bytes, byteorder, *, signed=False) -> int 39 40 Return the integer represented by the given array of bytes. 41 42 The bytes argument must be a bytes-like object (e.g. bytes or bytearray). 43 44 The byteorder argument determines the byte order used to represent the 45 integer. If byteorder is 'big', the most significant byte is at the 46 beginning of the byte array. If byteorder is 'little', the most 47 significant byte is at the end of the byte array. To request the native 48 byte order of the host system, use `sys.byteorder' as the byte order value. 49 50 The signed keyword-only argument indicates whether two's complement is 51 used to represent the integer. 52 """ 53 pass 54 55 def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 56 """ 57 int.to_bytes(length, byteorder, *, signed=False) -> bytes 58 59 Return an array of bytes representing an integer. 60 61 The integer is represented using length bytes. An OverflowError is 62 raised if the integer is not representable with the given number of 63 bytes. 64 65 The byteorder argument determines the byte order used to represent the 66 integer. If byteorder is 'big', the most significant byte is at the 67 beginning of the byte array. If byteorder is 'little', the most 68 significant byte is at the end of the byte array. To request the native 69 byte order of the host system, use `sys.byteorder' as the byte order value. 70 71 The signed keyword-only argument determines whether two's complement is 72 used to represent the integer. If signed is False and a negative integer 73 is given, an OverflowError is raised. 74 """ 75 pass 76 77 def __abs__(self, *args, **kwargs): # real signature unknown 78 """ abs(self) """ 79 pass 80 81 def __add__(self, *args, **kwargs): # real signature unknown 82 """ Return self+value. """ 83 pass 84 85 def __and__(self, *args, **kwargs): # real signature unknown 86 """ Return self&value. """ 87 pass 88 89 def __bool__(self, *args, **kwargs): # real signature unknown 90 """ self != 0 """ 91 pass 92 93 def __ceil__(self, *args, **kwargs): # real signature unknown 94 """ Ceiling of an Integral returns itself. """ 95 pass 96 97 def __divmod__(self, *args, **kwargs): # real signature unknown 98 #相除,得到商和餘數組成的元組 99 """ Return divmod(self, value). """ 100 pass 101 102 def __eq__(self, *args, **kwargs): # real signature unknown 103 """ Return self==value. """ 104 pass 105 106 def __float__(self, *args, **kwargs): # real signature unknown 107 """ float(self) """ 108 pass 109 110 def __floordiv__(self, *args, **kwargs): # real signature unknown 111 """ Return self//value. """ 112 pass 113 114 def __floor__(self, *args, **kwargs): # real signature unknown 115 """ Flooring an Integral returns itself. """ 116 pass 117 118 def __format__(self, *args, **kwargs): # real signature unknown 119 pass 120 121 def __getattribute__(self, *args, **kwargs): # real signature unknown 122 """ Return getattr(self, name). """ 123 pass 124 125 def __getnewargs__(self, *args, **kwargs): # real signature unknown 126 pass 127 128 def __ge__(self, *args, **kwargs): # real signature unknown 129 """ Return self>=value. """ 130 pass 131 132 def __gt__(self, *args, **kwargs): # real signature unknown 133 """ Return self>value. """ 134 pass 135 136 def __hash__(self, *args, **kwargs): # real signature unknown 137 """ Return hash(self). """ 138 pass 139 140 def __index__(self, *args, **kwargs): # real signature unknown 141 """ Return self converted to an integer, if self is suitable for use as an index into a list. """ 142 pass 143 144 def __init__(self, x, base=10): # known special case of int.__init__ 145 #構造方法 146 """ 147 int(x=0) -> integer 148 int(x, base=10) -> integer 149 150 Convert a number or string to an integer, or return 0 if no arguments 151 are given. If x is a number, return x.__int__(). For floating point 152 numbers, this truncates towards zero. 153 154 If x is not a number or if base is given, then x must be a string, 155 bytes, or bytearray instance representing an integer literal in the 156 given base. The literal can be preceded by '+' or '-' and be surrounded 157 by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. 158 Base 0 means to interpret the base from the string as an integer literal. 159 >>> int('0b100', base=0) 160 4 161 # (copied from class doc) 162 """ 163 pass 164 165 def __int__(self, *args, **kwargs): # real signature unknown 166 """ int(self) """ 167 pass 168 169 def __invert__(self, *args, **kwargs): # real signature unknown 170 """ ~self """ 171 pass 172 173 def __le__(self, *args, **kwargs): # real signature unknown 174 """ Return self<=value. """ 175 pass 176 177 def __lshift__(self, *args, **kwargs): # real signature unknown 178 """ Return self<<value. """ 179 pass 180 181 def __lt__(self, *args, **kwargs): # real signature unknown 182 """ Return self<value. """ 183 pass 184 185 def __mod__(self, *args, **kwargs): # real signature unknown 186 """ Return self%value. """ 187 pass 188 189 def __mul__(self, *args, **kwargs): # real signature unknown 190 """ Return self*value. """ 191 pass 192 193 def __neg__(self, *args, **kwargs): # real signature unknown 194 """ -self """ 195 pass 196 197 @staticmethod # known case of __new__ 198 def __new__(*args, **kwargs): # real signature unknown 199 """ Create and return a new object. See help(type) for accurate signature. """ 200 pass 201 202 def __ne__(self, *args, **kwargs): # real signature unknown 203 """ Return self!=value. """ 204 pass 205 206 def __or__(self, *args, **kwargs): # real signature unknown 207 """ Return self|value. """ 208 pass 209 210 def __pos__(self, *args, **kwargs): # real signature unknown 211 """ +self """ 212 pass 213 214 def __pow__(self, *args, **kwargs): # real signature unknown 215 """ Return pow(self, value, mod). """ 216 pass 217 218 def __radd__(self, *args, **kwargs): # real signature unknown 219 """ Return value+self. """ 220 pass 221 222 def __rand__(self, *args, **kwargs): # real signature unknown 223 """ Return value&self. """ 224 pass 225 226 def __rdivmod__(self, *args, **kwargs): # real signature unknown 227 """ Return divmod(value, self). """ 228 pass 229 230 def __repr__(self, *args, **kwargs): # real signature unknown 231 #轉化為解釋器可讀取的形式 232 """ Return repr(self). """ 233 pass 234 235 def __rfloordiv__(self, *args, **kwargs): # real signature unknown 236 """ Return value//self. """ 237 pass 238 239 def __rlshift__(self, *args, **kwargs): # real signature unknown 240 """ Return value<<self. """ 241 pass 242 243 def __rmod__(self, *args, **kwargs): # real signature unknown 244 """ Return value%self. """ 245 pass 246 247 def __rmul__(self, *args, **kwargs): # real signature unknown 248 """ Return value*self. """ 249 pass 250 251 def __ror__(self, *args, **kwargs): # real signature unknown 252 """ Return value|self. """ 253 pass 254 255 def __round__(self, *args, **kwargs): # real signature unknown 256 """ 257 Rounding an Integral returns itself. 258 Rounding with an ndigits argument also returns an integer. 259 """ 260 pass 261 262 def __rpow__(self, *args, **kwargs): # real signature unknown 263 """ Return pow(value, self, mod). """ 264 pass 265 266 def __rrshift__(self, *args, **kwargs): # real signature unknown 267 """ Return value>>self. """ 268 pass 269 270 def __rshift__(self, *args, **kwargs): # real signature unknown 271 """ Return self>>value. """ 272 pass 273 274 def __rsub__(self, *args, **kwargs): # real signature unknown 275 """ Return value-self. """ 276 pass 277 278 def __rtruediv__(self, *args, **kwargs): # real signature unknown 279 """ Return value/self. """ 280 pass 281 282 def __rxor__(self, *args, **kwargs): # real signature unknown 283 """ Return value^self. """ 284 pass 285 286 def __sizeof__(self, *args, **kwargs): # real signature unknown 287 """ Returns size in memory, in bytes """ 288 pass 289 290 def __str__(self, *args, **kwargs): # real signature unknown 291 """ Return str(self). """ 292 pass 293 294 def __sub__(self, *args, **kwargs): # real signature unknown 295 """ Return self-value. """ 296 pass 297 298 def __truediv__(self, *args, **kwargs): # real signature unknown 299 """ Return self/value. """ 300 pass 301 302 def __trunc__(self, *args, **kwargs): # real signature unknown 303 """ Truncating an Integral returns itself. """ 304 pass 305 306 def __xor__(self, *args, **kwargs): # real signature unknown 307 """ Return self^value. """ 308 pass 309 310 denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 311 """the denominator of a rational number in lowest terms""" 312 313 imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 314 """the imaginary part of a complex number""" 315 316 numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 317 """the numerator of a rational number in lowest terms""" 318 319 real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 320 """the real part of a complex number"""View Code
幾個常用的功能:
- __divmod__():
1 all_items = 95 2 pager = 10 3 result1 = all_items.__divmod__(10) 4 #__divmod__()功能常用於頁面分頁,如上共95個數據,每頁10個數據。 5 result2 = all_items.__rdivmod__(10) 6 print (result1,result2)
View Code - __init__(),構造方法:
1 >>> n = 19 2 >>> n = int(19) #這個過程python自動觸發__init__構造方法
float內部功能
1 class float(object): 2 """ 3 float(x) -> floating point number 4 5 Convert a string or number to a floating point number, if possible. 6 """ 7 def as_integer_ratio(self): # real signature unknown; restored from __doc__ 8 #獲取浮點數化為分數的最簡比 9 """ 10 float.as_integer_ratio() -> (int, int) 11 12 Return a pair of integers, whose ratio is exactly equal to the original 13 float and with a positive denominator. 14 Raise OverflowError on infinities and a ValueError on NaNs. 15 16 >>> (10.0).as_integer_ratio() 17 (10, 1) 18 >>> (0.0).as_integer_ratio() 19 (0, 1) 20 >>> (-.25).as_integer_ratio() 21 (-1, 4) 22 """ 23 pass 24 25 def conjugate(self, *args, **kwargs): # real signature unknown 26 """ Return self, the complex conjugate of any float. """ 27 pass 28 29 def fromhex(self, string): # real signature unknown; restored from __doc__ 30 """ 31 float.fromhex(string) -> float 32 33 Create a floating-point number from a hexadecimal string. 34 >>> float.fromhex('0x1.ffffp10') 35 2047.984375 36 >>> float.fromhex('-0x1p-1074') 37 -5e-324 38 """ 39 return 0.0 40 41 def hex(self): # real signature unknown; restored from __doc__ 42 """ 43 float.hex() -> string 44 45 Return a hexadecimal representation of a floating-point number. 46 >>> (-0.1).hex() 47 '-0x1.999999999999ap-4' 48 >>> 3.14159.hex() 49 '0x1.921f9f01b866ep+1' 50 """ 51 return "" 52 53 def is_integer(self, *args, **kwargs): # real signature unknown 54 """ Return True if the float is an integer. """ 55 pass 56 57 def __abs__(self, *args, **kwargs): # real signature unknown 58 """ abs(self) """ 59 pass 60 61 def __add__(self, *args, **kwargs): # real signature unknown 62 """ Return self+value. """ 63 pass 64 65 def __bool__(self, *args, **kwargs): # real signature unknown 66 """ self != 0 """ 67 pass 68 69 def __divmod__(self, *args, **kwargs): # real signature unknown 70 """ Return divmod(self, value). """ 71 pass 72 73 def __eq__(self, *args, **kwargs): # real signature unknown 74 """ Return self==value. """ 75 pass 76 77 def __float__(self, *args, **kwargs): # real signature unknown 78 """ float(self) """ 79 pass 80 81 def __floordiv__(self, *args, **kwargs): # real signature unknown 82 """ Return self//value. """ 83 pass 84 85 def __format__(self, format_spec): # real signature unknown; restored from __doc__ 86 """ 87 float.__format__(format_spec) -> string 88 89 Formats the float according to format_spec. 90 """ 91 return "" 92 93 def __getattribute__(self, *args, **kwargs): # real signature unknown 94 """ Return getattr(self, name). """ 95 pass 96 97 def __getformat__(self, typestr): # real signature unknown; restored from __doc__ 98 """ 99 float.__getformat__(typestr) -> string 100 101 You probably don't want to use this function. It exists mainly to be 102 used in Python's test suite. 103 104 typestr must be 'double' or 'float'. This function returns whichever of 105 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the 106 format of floating point numbers used by the C type named by typestr. 107 """ 108 return "" 109 110 def __getnewargs__(self, *args, **kwargs): # real signature unknown 111 pass 112 113 def __ge__(self, *args, **kwargs): # real signature unknown 114 """ Return self>=value. """ 115 pass 116 117 def __gt__(self, *args, **kwargs): # real signature unknown 118 """ Return self>value. """ 119 pass 120 121 def __hash__(self, *args, **kwargs): # real signature unknown 122 """ Return hash(self). """ 123 pass 124 125 def __init__(self, x): # real signature unknown; restored from __doc__ 126 pass 127 128 def __int__(self, *args, **kwargs): # real signature unknown 129 """ int(self) """ 130 pass 131 132 def __le__(self, *args, **kwargs): # real signature unknown