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连接mongodb操作数据示例(mongodb数据库配置类)
Dec 31 Python
python中引用与复制用法实例分析
Jun 04 Python
Python发送form-data请求及拼接form-data内容的方法
Mar 05 Python
python中json格式数据输出的简单实现方法
Oct 31 Python
Python利用splinter实现浏览器自动化操作方法
May 11 Python
Pytorch 实现数据集自定义读取
Jan 18 Python
python os模块在系统管理中的应用
Jun 22 Python
通过Python实现Payload分离免杀过程详解
Jul 13 Python
Pycharm常用快捷键总结及配置方法
Nov 14 Python
用Python自动清理系统垃圾的实现
Jan 18 Python
python基于机器学习预测股票交易信号
May 25 Python
Python采集爬取京东商品信息和评论并存入MySQL
Apr 12 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
用文本文件制作留言板提示(上)
2006/10/09 PHP
PHP cookie与session会话基本用法实例分析
2019/11/18 PHP
解决FireFox下[使用event很麻烦]的问题
2006/11/26 Javascript
谷歌浏览器不支持showModalDialog模态对话框的解决方法
2014/09/22 Javascript
js的flv视频播放器插件使用方法
2015/06/23 Javascript
jQuery Mobile中的button按钮组件基础使用教程
2016/05/23 Javascript
使用JS中的exec()方法构造正则表达式验证
2016/08/01 Javascript
js 调用百度分享功能
2017/02/27 Javascript
vue2.0多条件搜索组件使用详解
2020/03/26 Javascript
JS实现的简单标签点击切换功能示例
2017/09/21 Javascript
React Native开发封装Toast与加载Loading组件示例
2018/09/08 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
2019/09/17 Javascript
Vue axios 将传递的json数据转为form data的例子
2019/10/29 Javascript
Vue实现点击箭头上下移动效果
2020/06/11 Javascript
Echarts.js无法引入问题解决方案
2020/10/30 Javascript
[06:16]DOTA2守卫传承者——职业选手谈心路历程
2015/02/26 DOTA
python重试装饰器的简单实现方法
2019/01/31 Python
pandas数据集的端到端处理
2019/02/18 Python
python获取点击的坐标画图形的方法
2019/07/09 Python
python 绘制拟合曲线并加指定点标识的实现
2019/07/10 Python
python爬取盘搜的有效链接实现代码
2019/07/20 Python
pandas 对日期类型数据的处理方法详解
2019/08/08 Python
Django rstful登陆认证并检查session是否过期代码实例
2019/08/13 Python
python常见字符串处理函数与用法汇总
2019/10/30 Python
canvas三角函数模拟水波效果的示例代码
2018/07/03 HTML / CSS
美国最大的万圣节服装网站:HalloweenCostumes.com
2017/10/12 全球购物
Harrods美国:英国最大的百货公司
2018/11/04 全球购物
德国汽车零件和汽车配件网上商店:kfzteile24
2018/11/14 全球购物
荷兰鞋类购物网站:Donelli
2019/05/24 全球购物
电信营业员自我评价分享
2014/01/17 职场文书
课堂教学改革实施方案
2014/03/17 职场文书
村干部培训方案
2014/05/02 职场文书
巾帼志愿者活动方案
2014/08/17 职场文书
毕业生实习证明
2014/09/19 职场文书
2015年学校党建工作总结
2015/05/19 职场文书
2016年9月份红领巾广播稿
2015/12/21 职场文书