Python 数值类型 int bool float complex str 的属性和方法

函数 dir(object) 用于查找对象的属性和方法:

>>> dir(int)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

各数据类型的属性、方法的异同对比

>>> iList = dir(int)
>>> bList = dir(bool)
>>> fList = dir(float)
>>> iList == bList
True
>>> iList == fList
False

注:其中int和bool的dir()返回值列表完全相同 

列印5种数值类型的属性、方法列表

>>> typeList = ['int','bool','float','complex','str']
>>> for t in typeList:
	print(t+':')
	for i in range(len(dir(eval(t)))):
		print(dir(eval(t))[i])
	print()

注:int 和 bool 一样,str 与前四个差别较大。

类型 int、float、complex 属性、方法的对比

int float complex 注释
__pos__ __pos__ __pos__ 一元运算正号 +
__neg__ __neg__ __neg__ 一元运算负号 -
__abs__ __abs__ __abs__ abs() 绝对值
__add__ __add__ __add__ 加法 +
__sub__ __sub__ __sub__ 减法 -
__mul__ __mul__ __mul__ 乘法 *
__truediv__ __truediv__ __truediv__ 除法 /
__floordiv__ __floordiv__ __floordiv__ 整除 //
__mod__ __mod__ __mod__ 取余 %
__pow__ __pow__ __pow__ pow() 即**
__rmod__ __rmod__ __rmod__ 当右操作数支持此运算,而左操作数不支持此运算时使用
__divmod__ __divmod__ __divmod__ 同上
__rdivmod__ __rdivmod__ __rdivmod__ 同上
__radd__ __radd__ __radd__ 同上
__rsub__ __rsub__ __rsub__ 同上
__rmul__ __rmul__ __rmul__ 同上
__rpow__ __rpow__ __rpow__ 同上
__rtruediv__ __rtruediv__ __rtruediv__ 同上
__rfloordiv__ __rfloordiv__ __rfloordiv__ 同上
__eq__ __eq__ __eq__ x==y
__ne__ __ne__ __ne__ x!=y
__ge__ __ge__ __ge__ x>=y
__le__ __le__ __le__ x<=y
__gt__ __gt__ __gt__ x>y
__lt__ __lt__ __lt__ x<y
__int__ __int__ __int__ 强制转换类型
__float__ __float__ __float__ 强制转换类型
__bool__ __bool__ __bool__ 强制转换类型
__str__ __str__ __str__ 强制转换类型
__repr__ __repr__ __repr__ 同__str__ (在老版本中转换浮点数时精度不同)
__format__ __format__ __format__  
__init__ __init__ __init__  
__class__ __class__ __class__ 返回数据类型的初始空值
__init_subclass__ __init_subclass__ __init_subclass__  
__subclasshook__ __subclasshook__ __subclasshook__  
__new__ __new__ __new__ 创建实例
__hash__ __hash__ __hash__  
__doc__ __doc__ __doc__  
__sizeof__ __sizeof__ __sizeof__  
__dir__ __dir__ __dir__ dir()的子集
__reduce__ __reduce__ __reduce__  
__reduce_ex__ __reduce_ex__ __reduce_ex__  
__getattribute__ __getattribute__ __getattribute__  
__setattr__ __setattr__ __setattr__  
__delattr__ __delattr__ __delattr__  
conjugate conjugate conjugate  
real real real 实部
imag imag imag 虚部
as_integer_ratio as_integer_ratio    
__round__ __round__   四舍五入取整
__trunc__ __trunc__   舍尾取整
__getnewargs__   __getnewargs__  
__index__      
__rand__     随机数
__ceil__     向下取整
__floor__     向上取整
__invert__     一元运算~
__or__     或运算 |
__and__     与运算 &
__xor__     异或运算 ^
__lshift__     左移运算 <<
__rshift__     右移运算 >>
__ror__     当右操作数支持此运算,而左操作数不支持此运算时使用
__rxor__     同上
__rlshift__     同上
__rrshift__    

同上

from_bytes      
to_bytes      
bit_length      
numerator      
denominator      
  hex    
  fromhex    
  is_integer    
  __set_format__    
  __getformat__  

类型 str 属性、方法列表

以下为str特有的方法:

__len__    __iter__    __getitem__    __contains__ 

capitalize    casefold    center    count    

encode    endswith    expandtabs    find    

format    format_map    index    isalnum    

isalpha    isascii    isdecimal    isdigit    

isidentifier    islower    isnumeric    isprintable    

isspace    istitle    isupper    join    

ljust    lower    lstrip    maketrans    

partition    replace    rfind    rindex    

rjust    rpartition    rsplit    rstrip    

split    splitlines    startswith    strip    

swapcase    title    translate    upper    

zfill    

以下是通用方法,类同于 int 等数据类型的

__add__   __class__          __delattr__
__dir__          __doc__          __eq__
__format__          __ge__          __getattribute__
__getnewargs__          __gt__          __hash__
__init__          __init_subclass__          __le__
__lt__          __mod__          __mul__ 
__ne__          __new__          __reduce__
__reduce_ex__          __repr__          __rmod__
__rmul__          __setattr__          __sizeof__
__str__          __subclasshook__ 

具体说明见下回分晓......

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/115425651