python中有帮助函数吗
作者:silencement 发布时间:2021-06-15 15:50:02
标签:python,帮助函数
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.
来源:https://www.py.cn/faq/python/12517.html


猜你喜欢
- SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE function [dbo]
- 前言本文主要给大家介绍了关于Golang map生成有序json数据的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:先来看一段
- 一、现状Python 有诸多优秀的 Web 开发框架供我们使用,比如Django、Flask、Sanic。正常的情况下,我们基于这些 Web
- Tips:微信小程序可以在HbuilderX用HTML标签(如Ddiv、span等)写前端代码,也可以用微信小程序语法写(view、swip
- Python是一门非常酷的语言,因为很少的Python代码可以在短时间内做很多事情,并且,Python很容易就能支持多任务和多重处理。py&
- 一、场景描述在项目开发过程中,应用iframe实现外系统页面嵌套的时候,发现引入其他页面后,页面内容并未刷新加载,经过一番研究,解决方案如下
- 前言:不用改掉系统python2.7 ,原来是python2.7,我们还进行python2.7的保留1.编译前准备其他库的安装(使用sudo
- 引言:最近邻插值Nearest Neighbour Interpolate算法是图像处理中普遍使用的图像尺寸缩放算法,由于其实现简单计算速度
- asp之家注:在网页设计或编程中如何以最方便的方法来处理图片的宽高,以达到最佳的显示效果,这个问题相信很多网页制作人员都遇到过,最麻烦最费时
- 轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动
- kNN(k-nearest neighbor)是一种基本的分类与回归的算法。这里我们先只讨论分类中的kNN算法。k邻近算法的输入为实例的特征
- 一、前言如果你自己打印过东西,应该有过这种经历。如果用自己拍的图片,在手机上看感觉还是清晰可见,但是一打印出来就是漆黑一片。比如下面这两张图
- 1、django搜索路径使用 import 语句时,Python 所查找的系统目录清单。查看方式:import sysprint
- Python中print()函数的方法是打印指定的内容。在交互环境中输入“help(print)”指
- 这篇文章主要介绍了基于python实现把图片转换成素描,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 1.collections模块collections模块自Python 2.4版本开始被引入,包含了dict、set、list、tuple以
- 一、字符串方法1.字符串的分割s.split() 默认是按照空格分割s.split(',') 按照逗号分割(返回的是一个列表
- 不好意思,标题比较啰嗦,因为这次的流水账确实属于一个比较细节的小东西,下面详细讲:1需求最近在使用electron-vue 开发一个跨平台的
- 一个JavaScript代码编写的图片展示特效,效果很棒。效果图:演示:<!DOCTYPE HTML PUBLIC "-//
- 这篇文章主要介绍了Django项目基础配置和基本使用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需