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中使用hashlib模块处理算法的教程
Apr 28 Python
Python使用Redis实现作业调度系统(超简单)
Mar 22 Python
Python算法应用实战之栈详解
Feb 04 Python
Python批量查询域名是否被注册过
Jun 21 Python
Python单例模式的两种实现方法
Aug 14 Python
浅析Python中的赋值和深浅拷贝
Aug 15 Python
Python实现绘制双柱状图并显示数值功能示例
Jun 23 Python
Python如何使用OS模块调用cmd
Feb 27 Python
python3利用Axes3D库画3D模型图
Mar 25 Python
python批量检查两个对应的txt文件的行数是否一致的实例代码
Oct 31 Python
python 日志模块logging的使用场景及示例
Jan 04 Python
实例详解Python的进程,线程和协程
Mar 13 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
ADODB类使用
2006/11/25 PHP
配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境
2009/04/25 PHP
PHP 金额数字转换成英文
2010/05/06 PHP
php实现天干地支计算器示例
2014/03/14 PHP
基于jQuery制作迷你背词汇工具
2010/07/27 Javascript
7个JS基础知识总结
2014/03/05 Javascript
点击button获取text内容并改变样式的js实现
2014/09/09 Javascript
jquery实现左右滑动菜单效果代码
2015/08/27 Javascript
微信小程序滚动Tab实现左右可滑动切换
2017/08/17 Javascript
vue-router项目实战总结篇
2018/02/11 Javascript
JS改变页面颜色源码分享
2018/02/24 Javascript
Vue中插入HTML代码的方法
2018/09/21 Javascript
antd Upload 文件上传的示例代码
2018/12/14 Javascript
JS对象和字符串之间互换操作实例分析
2019/02/02 Javascript
vue+web端仿微信网页版聊天室功能
2019/04/30 Javascript
通过vue手动封装on、emit、off的代码详解
2019/05/29 Javascript
微信小程序iBeacon测距及稳定程序的实现解析
2019/07/31 Javascript
解决Vue动态加载本地图片问题
2019/10/09 Javascript
如何使用Javascript中的this关键字
2020/05/28 Javascript
js实现特别简单的钟表效果
2020/09/14 Javascript
[00:44]2016完美“圣”典 风云人物:Mikasa宣传片
2016/12/07 DOTA
Python使用urllib2获取网络资源实例讲解
2013/12/02 Python
利用Python破解验证码实例详解
2016/12/08 Python
django 链接多个数据库 并使用原生sql实现
2020/03/28 Python
python实现mask矩阵示例(根据列表所给元素)
2020/07/30 Python
filter使用python3代码进行迭代元素的实例详解
2020/12/03 Python
利物浦足球俱乐部官方商店(美国):Liverpool FC US
2019/10/09 全球购物
英国森林假期:Forest Holidays
2021/01/01 全球购物
毕业自荐信
2013/12/16 职场文书
医院深入开展党的群众路线教育实践活动实施方案
2014/08/27 职场文书
领导干部作风建设工作总结
2014/10/23 职场文书
2014年司法所工作总结
2014/11/22 职场文书
2014年学生管理工作总结
2014/12/20 职场文书
第二次离婚起诉书
2015/05/18 职场文书
党支部培养考察意见
2015/06/02 职场文书
何玥事迹观后感
2015/06/16 职场文书