函数 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()返回值列表完全相同
列印各数值类型的方法列表
>>> typeList = ['int','bool','float','complex','bytes','str']
>>> for t in typeList:
print(t+':')
for i in range(len(dir(eval(t)))):
print(dir(eval(t))[i])
print()
注:比较结果:int 和 bool 的列表完全一致,float、complex与int比较接近;bytes与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、 bytes 方法的比较
str | bytes | 注释或举列 |
capitalize | capitalize | 首字符大写 |
center | center | 在串的两头填充字符,默认用空格 |
count | count | 返回子串的个数 |
endswith | endswith | 是否存在后缀子串 |
expandtabs | expandtabs | 扩展 \t 字符的空格数,默认为8 |
find | find | 返回子串的位置索引 |
index | index | 返回子串的位置索引 |
isalnum | isalnum | 判断是否字母或数字 |
isalpha | isalpha | 判断是否字母 |
isascii | isascii | 判断是否字母 |
isdigit | isdigit | 判断是否字母 |
islower | islower | 判断是否小写 |
isspace | isspace | 判断是否空格 |
istitle | istitle | 判断串中各单词(空格分隔的)是否首字母大写 |
isupper | isupper | 判断是否大写 |
join | join | -- |
ljust | ljust | -- |
lower | lower | 全串的字母都转小写 |
lstrip | lstrip | 只消除串左边的空格 |
maketrans | maketrans | 创建字符映射转换表,与translate搭配使用 |
partition | partition | -- |
replace | replace | 替换掉部分子串 |
rfind | rfind | r+方法名的都为右向操作 |
rindex | rindex | -- |
rjust | rjust | -- |
rpartition | rpartition | -- |
rsplit | rsplit | -- |
rstrip | rstrip | 只消除串右边的空格 |
split | split | 用给定分隔符分割字符串 |
splitlines | splitlines | 用换行符(\r,\n,\r\n)分割字符串 |
startswith | startswith | 是否存在前缀子串 |
strip | strip | 消除串左右两边的空格 |
swapcase | swapcase | 串中各字母的大小写反转 |
title | title | 标题化串,把串的所有单词(空格分隔的)首字母转大写 |
translate | translate | 根据maketrans定义的转换表转换串中的字符 |
upper | upper | 全串的字母都转大写 |
zfill | zfill | 在串左边用0填充 |
casefold | ||
encode | ||
format | ||
format_map | ||
isdecimal | ||
isidentifier | ||
isnumeric | ||
isprintable | ||
decode | ||
fromhex | ||
hex |
注:黑色字体部分的方法与int类型的类同,蓝色部分是str和bytes两者共有但是int等类型没有的,红色则是str或bytes一方所特有的。
具体说明见下回分晓......