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 字符串定义
Sep 25 Python
使用pyecharts无法import Bar的解决方案
Apr 23 Python
python 3.7.0 安装配置方法图文教程
Aug 27 Python
PyQt5实现简易电子词典
Jun 25 Python
python控制台实现tab补全和清屏的例子
Aug 20 Python
python使用 __init__初始化操作简单示例
Sep 26 Python
Python Tensor FLow简单使用方法实例详解
Jan 14 Python
Python导入模块包原理及相关注意事项
Mar 25 Python
学习Python爬虫的几点建议
Aug 05 Python
python归并排序算法过程实例讲解
Nov 04 Python
python 用递归实现通用爬虫解析器
Apr 16 Python
python APScheduler执行定时任务介绍
Apr 19 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
PHP句法规则详解 入门学习
2011/11/09 PHP
新手菜鸟必读:session与cookie的区别
2013/08/22 PHP
php中用memcached实现页面防刷新功能
2014/08/19 PHP
Yii2简单实现给表单添加验证码的方法
2016/07/18 PHP
phpmailer绑定邮箱的实现方法
2016/12/01 PHP
Zend Framework入门应用实例详解
2016/12/11 PHP
PHP 中常量的知识整理
2017/04/14 PHP
浅析document.createDocumentFragment()与js效率
2013/07/08 Javascript
JavaScript验证电子邮箱的函数
2014/08/22 Javascript
详解jQuery中的元素的属性和相关操作
2015/08/14 Javascript
开启Javascript中apply、call、bind的用法之旅模式
2015/10/28 Javascript
JavaScript程序设计之JS调试
2015/12/09 Javascript
原生 JS Ajax,GET和POST 请求实例代码
2016/06/08 Javascript
微信小程序开发之好友列表字母列表跳转对应位置
2017/09/26 Javascript
js中的reduce()函数讲解
2019/01/18 Javascript
JavaScript强制类型转换和隐式类型转换操作示例
2019/05/01 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
微信公众号平台接口开发 获取access_token过程解析
2019/08/14 Javascript
对layui初始化列表的CheckBox属性详解
2019/09/13 Javascript
VUE+elementui面包屑实现动态路由详解
2019/11/04 Javascript
jQuery实现可编辑的表格
2019/12/11 jQuery
Python中常用操作字符串的函数与方法总结
2016/02/04 Python
tensorflow识别自己手写数字
2018/03/14 Python
删除python pandas.DataFrame 的多重index实例
2018/06/08 Python
python3实现字符串操作的实例代码
2019/04/16 Python
Python Collatz序列实现过程解析
2019/10/12 Python
python:动态路由的Flask程序代码
2019/11/22 Python
解决jupyter notebook打不开无反应 浏览器未启动的问题
2020/04/10 Python
css3过渡_动力节点Java学院整理
2017/07/11 HTML / CSS
HTML5中meta属性的使用方法
2016/02/29 HTML / CSS
ProBikeKit英国:在线公路自行车之家
2017/02/10 全球购物
香港家用健身器材、运动器材及健康美容仪器专门店:FitBoxx
2019/12/05 全球购物
个人求职简历的自我评价
2013/10/19 职场文书
多人股份制合作协议书
2016/03/19 职场文书
低端且暴利的线上线下创业项目分享
2019/09/03 职场文书
导游词之香港-太平山顶
2019/10/18 职场文书