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在命令行下使用google翻译(带语音)
Jan 16 Python
使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤
Jan 23 Python
Python快速排序算法实例分析
Nov 29 Python
用Eclipse写python程序
Feb 10 Python
Python闭包执行时值的传递方式实例分析
Jun 04 Python
pip 安装库比较慢的解决方法(国内镜像)
Oct 06 Python
wxpython实现按钮切换界面的方法
Nov 19 Python
运行tensorflow python程序,限制对GPU和CPU的占用操作
Feb 06 Python
python tkinter之 复选、文本、下拉的实现
Mar 04 Python
python中os包的用法
Jun 01 Python
在pycharm中debug 实时查看数据操作(交互式)
Jun 09 Python
Python实现淘宝秒杀功能的示例代码
Jan 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
PHP4实际应用经验篇(4)
2006/10/09 PHP
PHP调用MySQL的存储过程的实现代码
2008/08/12 PHP
PHP微信开发之查询城市天气
2016/06/23 PHP
PHP上传Excel文件导入数据到MySQL数据库示例
2016/10/25 PHP
关于文本限制字数的js代码
2007/04/02 Javascript
记录几个javascript有关的小细节
2007/04/02 Javascript
jquery、js调用iframe父窗口与子窗口元素的方法整理
2014/07/31 Javascript
JavaScript改变CSS样式的方法汇总
2015/05/07 Javascript
Javascript OOP之面向对象
2016/07/31 Javascript
canvas雪花效果核心代码分享
2017/02/19 Javascript
javascript基础练习之翻转字符串与回文
2017/02/20 Javascript
js实现带三角符的手风琴效果
2017/03/01 Javascript
webpack打包后直接访问页面图片路径错误的解决方法
2017/06/17 Javascript
vue.js源代码core scedule.js学习笔记
2017/07/03 Javascript
Angular2环境搭建具体操作步骤(推荐)
2017/08/04 Javascript
如何以Angular的姿势打开Font-Awesome详解
2018/04/22 Javascript
React组件重构之嵌套+继承及高阶组件详解
2018/07/19 Javascript
vue 组件的封装之基于axios的ajax请求方法
2018/08/11 Javascript
Vue组件跨层级获取组件操作
2020/07/27 Javascript
Python中使用dom模块生成XML文件示例
2015/04/05 Python
在Python的Django框架中调用方法和处理无效变量
2015/07/15 Python
Python实现简单的多任务mysql转xml的方法
2017/02/08 Python
Python延时操作实现方法示例
2018/08/14 Python
一文带你了解Python中的字符串是什么
2018/11/20 Python
Python字符串逆序的实现方法【一题多解】
2019/02/18 Python
Python 20行简单实现有道在线翻译的详解
2019/05/15 Python
new_zeros() pytorch版本的转换方式
2020/02/18 Python
自定义Django默认的sitemap站点地图样式
2020/03/04 Python
解决pycharm 格式报错tabs和space不一致问题
2021/02/26 Python
英国知名的护肤彩妆与时尚配饰大型综合零售电商:Unineed
2016/11/21 全球购物
公益活动邀请函
2014/02/05 职场文书
美丽家庭事迹材料
2014/05/03 职场文书
2015人事行政工作总结范文
2015/05/21 职场文书
消费者投诉书范文
2015/07/02 职场文书
带你学习MySQL执行计划
2021/05/31 MySQL
使用Redis做预定库存缓存功能
2022/04/02 Redis