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抓取模板之家的CSS模板
Mar 16 Python
解析Python中while true的使用
Oct 13 Python
遗传算法python版
Mar 19 Python
Python实现发送与接收邮件的方法详解
Mar 28 Python
Flask框架使用DBUtils模块连接数据库操作示例
Jul 20 Python
python3.6的venv模块使用详解
Aug 01 Python
Python使用循环神经网络解决文本分类问题的方法详解
Jan 16 Python
TensorFlow实现打印每一层的输出
Jan 21 Python
Python基于requests实现模拟上传文件
Apr 21 Python
python一些性能分析的技巧
Aug 30 Python
python文件排序的方法总结
Sep 13 Python
Pygame Draw绘图函数的具体使用
Nov 17 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
DOTA2【瓜皮时刻】Vol.91 RTZ山史最惨“矿难”
2021/03/05 DOTA
php中mysql操作buffer用法详解
2015/03/19 PHP
Zend Framework校验器Zend_Validate用法详解
2016/12/09 PHP
自制PHP框架之路由与控制器
2017/05/07 PHP
[JS]点出统计器
2020/10/11 Javascript
js onload事件不起作用示例分析
2013/10/09 Javascript
Node.js中require的工作原理浅析
2014/06/24 Javascript
详解Vue.js——60分钟组件快速入门(上篇)
2016/12/05 Javascript
JS实现物体带缓冲的间歇运动效果示例
2016/12/22 Javascript
老生常谈js-react组件生命周期
2017/05/02 Javascript
深入理解vue.js中$watch的oldvalue与newValue
2017/08/07 Javascript
详解vue axios中文文档
2017/09/12 Javascript
jQuery获取复选框选中的当前行的某个字段的值
2017/09/15 jQuery
vuejs使用递归组件实现树形目录的方法
2017/09/30 Javascript
vue2.0 中使用transition实现动画效果使用心得
2018/08/13 Javascript
微信开发之微信jssdk录音功能开发示例
2018/10/22 Javascript
vue webpack打包后图片路径错误的完美解决方法
2018/12/07 Javascript
详解在微信小程序的JS脚本中使用Promise来优化函数处理
2019/03/06 Javascript
[49:17]DOTA2-DPC中国联赛 正赛 Phoenix vs Dynasty BO3 第三场 1月26日
2021/03/11 DOTA
Python登录并获取CSDN博客所有文章列表代码实例
2017/12/28 Python
Python如何抓取天猫商品详细信息及交易记录
2018/02/23 Python
Python 中的range(),以及列表切片方法
2018/07/02 Python
Python双向循环链表实现方法分析
2018/07/30 Python
python实现石头剪刀布程序
2021/01/20 Python
详解python中list的使用
2019/03/15 Python
浅谈Python 钉钉报警必备知识系统讲解
2020/08/17 Python
维德科技C#面试题笔试题
2015/12/09 面试题
建筑学推荐信
2013/11/03 职场文书
优秀中学生事迹材料
2014/01/31 职场文书
仓管岗位职责范本
2014/02/08 职场文书
中秋晚会策划方案
2014/06/12 职场文书
七一建党日演讲稿
2014/09/05 职场文书
构建和谐校园倡议书
2015/01/19 职场文书
CSS 还能这样玩?奇思妙想渐变的艺术
2021/04/27 HTML / CSS
python基于tkinter实现gif录屏功能
2021/05/19 Python
JavaScript利用html5新方法操作元素类名详解
2021/11/27 Javascript