python dir查看模块属性和方法函数

python 内置方法有很多,无论是初学者还是精通python 的程序员都不能全部即住所有的方法。dir() 函数算的上是 Python 比较常用的也很好用的一个函数,它返回包含查询对象的所有属性和方法名称的列表。

1、dir() 内置函数的作用

直接使用 dir()函数可以看到所有已创建的变量,这些已经创建的变量会保存在globals全局中。也通过输入形参方式可以查看对象内的所有的属性和方法,在 python 中任何东西都是对象,一种数据类型,一个模块等,都有子集的属性和方法,除了常用的方法外,其他的你不需要全部记住它,直接使用 dir() 函数即可。

2、dir() 函数的使用方法

dir()函数操作方法很简单,只需要把你想要查询的对象填写在括号() 中就可以了。

例如:你想查看一些列表都有那些方法,你可以在括号()中直接传入空列表对象[]或是一个表示列表数据类型的变量名,像下面这样操作:
如果你想查看字符串,只要在()里填入参数变量 “” 即可。

>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

如何用 dir() 函数查看模块的属性和方法

要产看某个模块可干什么,先要导入模块,之后用上面讲过的方法查看就可以了,比如要查看 sys模块都可以干什么,可以想下面这样操作:

>>> import sys
>>> dir(sys)
['__breakpointhook__',
 '__displayhook__',
 '__doc__',
 '__excepthook__',
 '__interactivehook__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '__stderr__',
 '__stdin__',
 '__stdout__',
 '_clear_type_cache',
 '_current_frames',
 '_debugmallocstats',
 '_framework',
 '_getframe',
 '_git',
 '_home',
 '_xoptions',
 'abiflags',
 'api_version',
 'argv',
 'base_exec_prefix',
 'base_prefix',
 'breakpointhook',
 'builtin_module_names',
 'byteorder',
 'call_tracing',
 'callstats',
 'copyright',
 'displayhook',
 'dont_write_bytecode',
 'exc_info',
 'excepthook',
 'exec_prefix',
 'executable',
 'exit',
 'flags',
 'float_info',
 'float_repr_style',
 'get_asyncgen_hooks',
 'get_coroutine_origin_tracking_depth',
 'get_coroutine_wrapper',
 'getallocatedblocks',
 'getcheckinterval',
 'getdefaultencoding',
 'getdlopenflags',
 'getfilesystemencodeerrors',
 'getfilesystemencoding',
 'getprofile',
 'getrecursionlimit',
 'getrefcount',
 'getsizeof',
 'getswitchinterval',
 'gettrace',
 'hash_info',
 'hexversion',
 'implementation',
 'int_info',
 'intern',
 'is_finalizing',
 'maxsize',
 'maxunicode',
 'meta_path',
 'modules',
 'path',
 'path_hooks',
 'path_importer_cache',
 'platform',
 'prefix',
 'ps1',
 'ps2',
 'ps3',
 'set_asyncgen_hooks',
 'set_coroutine_origin_tracking_depth',
 'set_coroutine_wrapper',
 'setcheckinterval',
 'setdlopenflags',
 'setprofile',
 'setrecursionlimit',
 'setswitchinterval',
 'settrace',
 'stderr',
 'stdin',
 'stdout',
 'thread_info',
 'version',
 'version_info',
 'warnoptions']

查询模块内置函数的方法:

问题1: 如何查看一个模块中方法是否在内置模块有定义?

用dir(模块名)看是否有'__builtins__'属性。

例如:查看string模块

>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

问题2、如何查看内置模块中内置函数定义

dir(__builtins__)


作者:Panda_phc
链接:https://www.jianshu.com/p/80faba0bd4ff

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值