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 17 Python
使用Python编写类UNIX系统的命令行工具的教程
Apr 15 Python
Python闭包实现计数器的方法
May 05 Python
python编程测试电脑开启最大线程数实例代码
Feb 09 Python
python保存数据到本地文件的方法
Jun 23 Python
python与字符编码问题
May 24 Python
利用django model save方法对未更改的字段依然进行了保存
Mar 28 Python
django 实现手动存储文件到model的FileField
Mar 30 Python
python 连续不等式语法糖实例
Apr 15 Python
Python魔术方法专题
Jun 19 Python
Django封装交互接口代码
Jul 12 Python
python爬虫请求库httpx和parsel解析库的使用测评
May 10 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数据入库前清理 注意php intval与mysql的int取值范围不同
2010/12/12 PHP
destoon找回管理员密码的方法
2014/06/21 PHP
php中eval函数的危害与正确禁用方法
2014/06/30 PHP
PHP curl实现抓取302跳转后页面的示例
2014/07/04 PHP
PHP7新特性foreach 修改示例介绍
2016/08/26 PHP
详解thinkphp中的volist标签
2018/01/15 PHP
PHP 计算两个特别大的整数实例代码
2018/05/07 PHP
php 使用html5 XHR2实现上传文件与进度显示功能示例
2020/03/03 PHP
PHP中类与对象功能、用法实例解读
2020/03/27 PHP
wap浏览自动跳转到wap页面的js代码
2014/05/17 Javascript
javascript 数组操作详解
2015/01/29 Javascript
jQuery模仿阿里云购买服务器选择购买时间长度的代码
2016/04/29 Javascript
模拟javascript中的sort排序(简单实例)
2016/08/17 Javascript
解决URL地址中的中文乱码问题的办法
2017/02/10 Javascript
Angularjs渲染的 using 指令的星级评分系统示例
2017/11/09 Javascript
5 种JavaScript编码规范
2018/01/30 Javascript
Vue2.0用户权限控制解决方案的示例
2018/02/10 Javascript
浅谈vue 单文件探索
2018/09/05 Javascript
详解Vue中watch对象内属性的方法
2019/02/01 Javascript
原生js实现获取form表单数据代码实例
2019/03/27 Javascript
Vue 递归多级菜单的实例代码
2019/05/05 Javascript
微信小程序和百度的语音识别接口详解
2019/05/06 Javascript
JQuery获得内容和属性方法解析
2020/05/30 jQuery
详解python之简单主机批量管理工具
2017/01/27 Python
shell命令行,一键创建 python 模板文件脚本方法
2018/03/20 Python
python tkinter之 复选、文本、下拉的实现
2020/03/04 Python
Django def clean()函数对表单中的数据进行验证操作
2020/07/09 Python
CSS3 3D酷炫立方体变换动画的实现
2019/03/26 HTML / CSS
很酷的HTML5电子书翻页动画特效
2016/02/25 HTML / CSS
美国最大网上鞋店:Zappos
2016/07/25 全球购物
异步传递消息系统的作用
2016/05/01 面试题
Python使用openpyxl复制整张sheet
2021/03/24 Python
复核员上岗演讲稿
2014/01/05 职场文书
规范化管理年活动总结
2014/08/29 职场文书
php将xml转化对象的实例详解
2021/11/17 PHP
vue中的可拖拽宽度div的实现示例
2022/04/08 Vue.js