更多课程 选择中心


Python培训

400-996-5531

Python中dir()、help()的使用

  • 发布:Python培训
  • 来源:Python3X
  • 时间:2019-08-14 16:02

Python中dir()、help()的使用

Python中的内置方法有很多,今天简单的说一下dir和helo的用法。当我们需要使用某些函数或者模块,对于内部的有些函数不清楚时,可以通过这两个函数进行查看具体的用法。

dir(): 用于查看对象内的属性以及方法。

help(): 用于查看属性和方法的详细说明

dir

查看str这个对象内的属性和方法

>>> dir(str)['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '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']>>>

当dir不带参数执行时,返回当前范围内的变量、方法和属性列表>>> dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

help

查看help属性和方法的详细说明,可通过Enter键加载更多。

>>> help('str')

Help on class str in module builtins:

class str(object)

| str(object='') -> str

| str(bytes_or_buffer[, encoding[, errors]]) -> str

| | Create a new string object from the given object. If encoding or

| errors is specified, then the object must expose a data buffer

| that will be decoded using the given encoding and error handler.

| Otherwise, returns the result of object.__str__() (if defined) | or repr(object).

| encoding defaults to sys.getdefaultencoding().

| errors defaults to 'strict'.

| | Methods defined here:

| | __add__(self, value, /)

| Return self+value.

| | __contains__(self, key, /)

| Return key in self.

| | __eq__(self, value, /)

| Return self==value.

| | __format__(self, format_spec, /)

| Return a formatted version of the string as described by format_spec.

| | __ge__(self, value, /)

| Return self>=value. | | __getattribute__(self, name, /)

| Return getattr(self, name).

| | __getitem__(self, key, /)

| Return self[key]. | | __getnewargs__(...) | | __gt__(self, value, /)

| Return self>value. | | __hash__(self, /)

| Return hash(self). | | __iter__(self, /)

| Implement iter(self). | | __le__(self, value, /)

| Return self<=value. | | __len__(self, /)

| Return len(self). | | __lt__(self, value, /)

| Return self<value. | | __mod__(self, value, /)

| Return self%value.

-- More --

>>>

当不带参数是 help() 则进入内置的帮助系统,可以在帮助系统内输入对象名进行查看。

>>> help() # 进入帮助系统

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at #/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type

"modules", "keywords", "symbols", or "topics". Each module also comes

with a one-line summary of what it does; to list the modules whose name

or summary contain a given string such as "spam", type "modules spam".

help> str # 查看str的详细说明

Help on class str in module builtins:

class str(object)

| str(object='') -> str

| str(bytes_or_buffer[, encoding[, errors]]) -> str

| | Create a new string object from the given object. If encoding or

| errors is specified, then the object must expose a data buffer

| that will be decoded using the given encoding and error handler.

| Otherwise, returns the result of object.__str__() (if def

免责声明:内容和图片源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

预约申请免费试听课

填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:使用Python制作表白小程序
下一篇:Python实现降雪效果

Python IDE推荐7个你可能会错过的Python IDE

Python面试题之Python中爬虫框架或模块的区别

2021年Python面试题及答案汇总详解

python数据分析,你需要这些工具

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省