python int对象

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.

| 将一个数字或字符串转换成整数,没有参数的时候为默认值0。如果参数时数字,调用__init__(),参数为浮点数,会发生截取。

|当x参数时不是数字时,或者有参数base,那么x参数一定是字面值是整数的字符串,字节流,或者是字节数组。这个字面值可以由正负号,前后可以由空格。

|base参数默认是10,base允许是0,2到36。如果base是0的时候,会根据字符串的字面值判断base的值。

1 a = int(‘100‘, 2)
2 print(a)
3 a = int(‘0b100‘, 0)
4 print(a)
1 4
2 4

| >>> int(‘0b100‘, base=0)
| 4
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)

1 print(abs(-10))
2 print(abs(10))
1 10
2 10

|
| __add__(self, value, /)
| Return self+value.

1 print(10 + 10 + 10)
1 30

|
| __and__(self, value, /)
| Return self&value.

1 print(1 & 3)
1 1

|
| __bool__(self, /)
| self != 0

1 if 1:
2     print("True")
3 if 0:
4     pass
5 else:
6     print("False")
1 True
2 False

|
| __ceil__(...)
| Ceiling of an Integral returns itself.

返回一个大于或者等于x的最小整数。

1 import math
2 print(math.ceil(10))
3 print(math.ceil(9.2))
4 print(math.ceil(-8.2))
1 10
2 10
3 -8

|
| __divmod__(self, value, /)
| Return divmod(self, value).

返回一个元组,一个值是x//y,第二个值是x%y。

1 print(divmod(10, 3))
1 (3, 1)

|
| __eq__(self, value, /)
| Return self==value.

1 print(5 == 5)
1 True

|
| __float__(self, /)
| float(self)

1 print(float(5))
1 5.0

|
| __floor__(...)
| Flooring an Integral returns itself.
|
| __floordiv__(self, value, /)
| Return self//value.
|
| __format__(...)
| default object formatter
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getnewargs__(...)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __index__(self, /)
| Return self converted to an integer, if self is suitable for use as an index into a list.
|
| __int__(self, /)
| int(self)
|
| __invert__(self, /)
| ~self
|
| __le__(self, value, /)
| Return self<=value.
|
| __lshift__(self, value, /)
| Return self<<value.
|
| __lt__(self, value, /)
| Return self<value.
|
| __mod__(self, value, /)
| Return self%value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __neg__(self, /)
| -self
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __or__(self, value, /)
| Return self|value.
|
| __pos__(self, /)
| +self
|
| __pow__(self, value, mod=None, /)
| Return pow(self, value, mod).
|
| __radd__(self, value, /)
| Return value+self.
|
| __rand__(self, value, /)
| Return value&self.
|
| __rdivmod__(self, value, /)
| Return divmod(value, self).
|
| __repr__(self, /)
| Return repr(self).
|
| __rfloordiv__(self, value, /)
| Return value//self.
|
| __rlshift__(self, value, /)
| Return value<<self.
|
| __rmod__(self, value, /)
| Return value%self.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __ror__(self, value, /)
| Return value|self.
|
| __round__(...)
| Rounding an Integral returns itself.
| Rounding with an ndigits argument also returns an integer.
|
| __rpow__(self, value, mod=None, /)
| Return pow(value, self, mod).
|
| __rrshift__(self, value, /)
| Return value>>self.
|
| __rshift__(self, value, /)
| Return self>>value.
|
| __rsub__(self, value, /)
| Return value-self.
|
| __rtruediv__(self, value, /)
| Return value/self.
|
| __rxor__(self, value, /)
| Return value^self.
|
| __sizeof__(...)
| Returns size in memory, in bytes
|
| __str__(self, /)
| Return str(self).
|
| __sub__(self, value, /)
| Return self-value.
|
| __truediv__(self, value, /)
| Return self/value.
|
| __trunc__(...)
| Truncating an Integral returns itself.
|
| __xor__(self, value, /)
| Return self^value.
|
| bit_length(...)
| int.bit_length() -> int
|
| Number of bits necessary to represent self in binary.
| >>> bin(37)
| ‘0b100101‘
| >>> (37).bit_length()
| 6
|
| conjugate(...)
| Returns self, the complex conjugate of any int.
|
| from_bytes(...) from builtins.type
| 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.
|
| to_bytes(...)
| 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.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| denominator
| the denominator of a rational number in lowest terms
|
| imag
| the imaginary part of a complex number
|
| numerator
| the numerator of a rational number in lowest terms
|
| real
| the real part of a complex number

原文地址:https://www.cnblogs.com/gundan/p/8137207.html

时间: 2024-07-31 13:33:29

python int对象的相关文章

【Python笔记】从一个“古怪”的case探究CPython对Int对象的实现细节

1. Python的对象模型 我们知道,在Python的世界里,万物皆对象(Object).根据Python官方文档对Data Model的说明,每个Python对象均拥有3个特性:身份.类型和值. 官方文档关于对象模型的这段概括说明对于我们理解Python对象是如此重要,所以本文将其摘录如下(为了使得结构更清晰,这里把原文档做了分段处理): 1) Every object has an identity, a type and a value. 2) An object's identity

python string 对象

上一篇的整数对象是定长数据,即C中的long型整数. 但是python中还大量存在着不定长的数据类型.如字符串对象 和整数对象一样,字符串对象的组成如下: PyStringObject定义如下: Ob_size就是实际使用的内存大小,在注释中:ob_sval contains space for 'ob_size+1' elements. 因为末尾需要额外包含一个0 byte来保证字符串的结束. 这个学过C语言的应该都不会陌生的.但是这只是一个字符数组,怎么能包含N个字符呢? 显然这只能是一个字

python 获取对象信息

当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >>> type(123) <type 'int'> >>> type('str') <type 'str'> >>> type(None) <type 'NoneType'> 如果一个变量指向函数或者类,也可以用type()判断: >&

python int异常 python isdigit

python int是python把任何类型转换成int类型的方法,但是你如果运用不好的话,会引发异常,但是python的str字符串转换方法运用起来倒是比较安全,它把任何对象转换成字符串类型都不会报异常. 现给个python int的例子: 比如a = '123' b = int(a) print b的结果是123 a = 'abc' b = int(a)  print b的话, 会报:ValueError: invalid literal for int() with base 10: 'f

Python 不同对象比较大小

万恶的源泉: Fireboo的疑问(当然 lambda 本身写的就有问题): >>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, 3 ] ) [[1, 2, 3], 3] ?: >>> 1 < [ 1 ] True >>> int < list True >>> dict < int < list True >>> int < map

Python - 面对对象(其他相关,异常处理,反射,单例模式,等..)

目录 Python - 面对对象(其他相关,异常处理,反射,等..) 一.isinstance(obj, cls) 二.issubclass(sub, super) 三.异常处理 1. 异常处理 2.异常种类 3.异常其他结构 4.主动触发异常 5.自定义异常 6.断言 四.反射 五. 单例模式 Python - 面对对象(其他相关,异常处理,反射,等..) 一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 class Foo(object): pass ob

从内存上看python的对象

python中有一个说法:一切皆是对象,怎么理解这句话呢?我们可以通过查看数字,字符串在内存中的表示形式来对这句话有个更深的认识. 那么,怎么查看对象在内存中是什么样的呢?可以先参考一些这篇文章:https://www.cnblogs.com/wujiecong/p/11583540.html 1.python的数字在内存中是什么样的? #python3.7 from ctypes import string_at from sys import getsizeof from binascii

Python之对象的属性

# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之对象的属性 #http://python.jobbole.com/82622/ #对象的属性 class bird(): feather = True class chicken(bird): fly = False def __init__(self,age): self.age = age summer = chicken(2) print(bird.__dict__) print(ch

python函数对象

适用于python 2.x版本 1. lambda函数 1 func = lambda x, y : x + y 2 print func(2, 4) lambda生成一个函数对象,参数是x,y, 返回x+y. 2. map() 函数 1 rtn = map((lambda x: x*3), [1, 2, 3, 4, 5, 6]) 2 print rtn map是python内置函数,第一个参数是函数,第二个参数是一个序列.第一个函数作用与序列的每一个元素,并将结果放在序列rtn里. 3. re