python中有帮助函数吗


Posted in Python onJune 19, 2020

python中的dir()函数是一个非常重要的函数,它可以帮助我们查看函数的功能和特性。

中文说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。

参数object: 对象、变量、类型。

版本:该函数在python各个版本中都有,但是每个版本中显示的属性细节有所不同。使用时注意区别。

例如

>>>import struct
>>>dir() # show the names in the module namespace
['__builtins__','__doc__','__name__','struct']
>>>dir(struct) # show the names in the struct module
['Struct','__builtins__','__doc__','__file__','__name__',
 '__package__','_clearcache','calcsize','error','pack','pack_into',
 'unpack','unpack_from']
>>>class Shape(object):
    def __dir__(self):
      return ['area','perimeter','location']
>>> s= Shape()
>>>dir(s)
['area', 'perimeter', 'location']
Note Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries 
to supply an 
interesting set of names more than it tries to supply a rigorously or consistently defined set of 
names, and its 
detailed behavior may change across releases. For example, metaclass attributes are not in the result 
list when the 
argument is a class.

 代码实例

>>>dir()
['__builtins__','__doc__','__name__','__package__']
>>>import struct
>>>dir()
['__builtins__','__doc__','__name__','__package__','struct']
>>>dir(struct)
['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack',
'pack_into','unpack','unpack_from']
>>>class Person(object):
...  def __dir__(self):
...      return ["name","age","country"]
...
>>>dir(Person)
['__class__','__delattr__','__dict__','__dir__','__doc__','__format__','__getattribute__','__hash__','__init__',
'__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__',
'__weakref__']
>>> tom= Person()
>>>dir(tom)
['age','country','name']

知识点扩展:

help()函数的作用

在使用python来编写代码时,会经常使用python自带函数或模块,一些不常用的函数或是模块的用途不是很清楚,这时候就需要用到help函数来查看帮助。

这里要注意下,help()函数是查看函数或模块用途的详细说明,而dir()函数是查看函数或模块内的操作方法都有什么,输出的是方法列表。

怎么使用help函数查看python模块中函数的用法

help()括号内填写参数,操作方法很简单。例如:

>>> help('dir')
Help on built-in function dir in module builtins:
dir(...)
  dir([object]) -> list of strings

  If called without an argument, return the names in the current scope.
  Else, return an alphabetized list of names comprising (some of) the attribut
es
  of the given object, and of attributes reachable from it.
  If the object supplies a method named __dir__, it will be used; otherwise
  the default dir() logic is used and returns:
   for a module object: the module's attributes.
   for a class object: its attributes, and recursively the attributes
    of its bases.
   for any other object: its attributes, its class's attributes, and
    recursively the attributes of its class's base classes.

到此这篇关于python中有帮助函数吗的文章就介绍到这了,更多相关python帮助函数详解内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python赋值操作方法分享
Mar 23 Python
Python计算三角函数之asin()方法的使用
May 15 Python
深入学习Python中的装饰器使用
Jun 20 Python
python 线程的暂停, 恢复, 退出详解及实例
Dec 06 Python
Python 装饰器实现DRY(不重复代码)原则
Mar 05 Python
数据清洗--DataFrame中的空值处理方法
Jul 03 Python
在Pycharm terminal中字体大小设置的方法
Jan 16 Python
python写日志文件操作类与应用示例
Jul 01 Python
Python利用FFT进行简单滤波的实现
Feb 26 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
Sep 29 Python
Python 远程开关机的方法
Nov 18 Python
Python基于Faker假数据构造库
Nov 30 Python
python中导入 train_test_split提示错误的解决
Jun 19 #Python
python中get和post有什么区别
Jun 19 #Python
python中setuptools的作用是什么
Jun 19 #Python
python怎么判断模块安装完成
Jun 19 #Python
Keras SGD 随机梯度下降优化器参数设置方式
Jun 19 #Python
python支持多继承吗
Jun 19 #Python
python和php哪个容易学
Jun 19 #Python
You might like
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
PHP中使用Memache作为进程锁的操作类分享
2015/03/30 PHP
PHP实现的pdo连接数据库并插入数据功能简单示例
2019/03/30 PHP
php面向对象程序设计中self与static的区别分析
2019/05/21 PHP
JS拖动技术 关于setCapture使用
2010/12/09 Javascript
JS中的form.submit()不能提交表单的错误原因
2014/10/08 Javascript
使用C++为node.js写扩展模块
2015/04/22 Javascript
分享自己用JS做的扫雷小游戏
2016/02/17 Javascript
jQuery获取单击节点对象的方法
2016/06/02 Javascript
jQuery包裹节点用法完整示例
2016/09/13 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
canvas 实现中国象棋
2017/02/17 Javascript
js获取json中key所对应的value值的简单方法
2020/06/17 Javascript
jQuery滑动效果实现方法分析
2018/09/05 jQuery
JavaScript实现表单注册、表单验证、运算符功能
2018/10/15 Javascript
js嵌套的数组扁平化:将多维数组变成一维数组以及push()与concat()区别的讲解
2019/01/19 Javascript
详解webpack的文件监听实现(热更新)
2020/09/11 Javascript
[00:12]2018DOTA2亚洲邀请赛 sylar表现SOLO技艺
2018/04/06 DOTA
[47:52]完美世界DOTA2联赛PWL S2 PXG vs InkIce 第二场 11.26
2020/11/30 DOTA
Python多线程编程(七):使用Condition实现复杂同步
2015/04/05 Python
Python中super的用法实例
2015/05/28 Python
Python 忽略文件名编码的方法
2020/08/01 Python
Sport-Thieme荷兰:购买体育用品
2019/08/25 全球购物
一家专门经营包包的英国网站:MyBag
2019/09/08 全球购物
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
请写出 BOOL flag 与"零值"比较的 if 语句
2016/02/29 面试题
怎样在 Applet 中建立自己的菜单(MenuBar/Menu)?
2012/06/20 面试题
介绍一下Java的事务处理
2012/12/07 面试题
三个Unix的命令面试题
2015/04/12 面试题
水务局局长岗位职责
2013/11/28 职场文书
初中学校军训方案
2014/05/09 职场文书
企业党员一句话承诺
2014/05/30 职场文书
企业文化口号
2014/06/12 职场文书
大学考试作弊检讨书
2015/05/06 职场文书
分享MySQL常用 内核 Debug 几种常见方法
2022/03/17 MySQL
ipad隐藏软件app图标方法
2022/04/19 数码科技