(1)内置函数dir()用来查看对象的成员。在Python中所有的一切都是对象,除了整数、实数、复数、字符串、列表、元组、字典、集合等等,还有range对象、enumerate对象、zip对象、filter对象、map对象等等,函数也是对象,类也是对象,模块也是对象。这样的话,dir()的用武之地就大了。

>>> dir(3)  #查看整数类型的成员,这里省略了输出结果

>>> dir('a') #查看字符串类型的成员

>>> import math
>>> dir(math) #查看math模块的成员

>>> def demo():pass #定义一个空函数

>>> dir(demo) #查看函数的成员

>>> class Demo: #定义一个类
     def __init__(self):pass
     def methodA(self):pass
     def methodB(self):pass

>>> dir(Demo) #查看类的成员
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'methodA', 'methodB']
>>> d = Demo() #实例化一个对象
>>> dir(d) #查看对象成员
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'methodA', 'methodB']

(2)内置函数help()用来返回对象的帮助信息,尤其常用来查看函数或对象方法的帮助信息。除此之外,help()还可以做很多事,例如查看模块的帮助信息,以及Python关键字和运算符的信息。

>>> help(sum)  #查看内置函数的帮助文档
Help on built-in function sum in module builtins:

sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
   
    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.

>>> help(3)  #查看整数类型的帮助文档
Help on int object:

class int(object)
 |  int(x=0) -> integer
 |  int(x, base=10) -> integer

...(其他输出结果略去)

>>> help('math')  #查看标准库的帮助文档,注意要加引号
Help on built-in module math:

NAME
    math

DESCRIPTION
    This module is always available.  It provides access to the
    mathematical functions defined by the C standard.

...(其他输出结果略去)

>>> help('')  #查看字符串类型的帮助文档
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str

...(其他输出结果略去)

>>> import math
>>> help(math.sin)  #查看标准库函数的帮助文档
Help on built-in function sin in module math:

sin(...)
    sin(x)
   
    Return the sine of x (measured in radians).

>>> help()   #进入帮助环境

Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help>

>>> help('in')  #查看Python关键字的帮助文档
Membership test operations
**************************

The operators "in" and "not in" test for membership.  "x in s"
evaluates to true if *x* is a member of *s*, and false otherwise.  "x
not in s" returns the negation of "x in s".  All built-in sequences
and set types support this as well as dictionary, for which "in" tests
whether the dictionary has a given key. For container types such as
list, tuple, set, frozenset, dict, or collections.deque, the
expression "x in y" is equivalent to "any(x is e or x == e for e in
y)".

...(其他输出结果略去)

>>> help('with')  #查看Python关键字with的帮助文档
The "with" statement
********************

The "with" statement is used to wrap the execution of a block with
methods defined by a context manager (see p With Statement
Context Managers). This allows common "try"..."except"..."finally"
usage patterns to be encapsulated for convenient reuse.

with_stmt ::= "with" with_item ("," with_item)* ":" suite
   with_item ::= expression ["as" target]

...(其他输出结果略去)

>>> help('for')  #查看Python关键字for的帮助文档
The "for" statement
*******************

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

for_stmt ::= "for" target_list "in" expression_list ":" suite
                ["else" ":" suite]

...(其他输出结果略去)

>>> help('+')  #查看所有运算符的帮助文档
Operator precedence
*******************

The following table summarizes the operator precedence in Python, from
lowest precedence (least binding) to highest precedence (most
binding).  Operators in the same box have the same precedence.  Unless
the syntax is explicitly given, operators are binary.  Operators in
the same box group left to right (except for exponentiation, which
groups from right to left).

...(其他输出结果略去)

那么问题来了,如果我们自己设计了函数或类或模块,如何能够通过help()给出帮助文档呢?答案是写注释。

>>> def add(x, y):  #定义函数,编写注释
     '''Accept integers x and y, return x+y'''
     return x+y

>>> help(add)  #查看自定义函数的帮助文档
Help on function add in module __main__:

add(x, y)
    Accept integers x and y, return x+y

>>> class T:
     '''This is a test.'''
     pass

>>> help(T)  #查看自定义类的帮助文档
Help on class T in module __main__:

class T(builtins.object)
 |  This is a test.
 | 
 |  Data descriptors defined here:
 | 
 |  __dict__
 |      dictionary for instance variables (if defined)
 | 
 |  __weakref__
 |      list of weak references to the object (if defined)

学习Python的利器:内置函数dir()和help()相关推荐

  1. python学习高级篇(part6)--内置函数dir

    学习笔记,仅供参考,有错必纠 内置函数dir 对于类对象或实例对象,可以调用内置函数dir()获得其所有可以访问的属性和方法(包括从父类中继承的属性和方法)的列表. 类对象与实例对象的结果是有区别的, ...

  2. python中dir用法_Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: >>> help(dir) Help on built-in function dir in mo ...

  3. [云炬python学习笔记]Numpy中内置函数min(),max(),sum()与Python中内置函数min(),max(),sum()性能对比分析

    众所周知,Python有许多内置函数(例如min(),max(),sum()),Numpy也有自己的内置函数(np.min(),np.max(),np.sum()).由于Numpy的函数是在编译码中执 ...

  4. python之路——内置函数和匿名函数

    楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...

  5. python 两个内置函数——locals 和globals(名字空间)批量以自定义变量名创建对象

    文章目录 locals 和globals(名字空间)简介 1.局部变量函数locals例子(locals 返回一个名字/值对的字典) 批量创建对象 示例1 示例2 函数内 类内 2.全局变量函数glo ...

  6. python中比较重要的几个函数_Python 几个重要的内置函数 python中的内置函数和关键字需要背过吗...

    python重要的几个内置函数用法 python内置函数什么用忘不掉的是回忆,继续的是生活,错过的,就当是路过吧.来来往往身边出现很多人,总有一个位置,一直没有变.看看温暖的阳光,偶尔还是会想一想. ...

  7. [转载] (三)Python关键字和内置函数

    参考链接: Python中的数学函数 4(特殊函数和常量) 一.Python的关键字 和其他语言一样,关键字有特殊含义,并且关键字不能作为变量名.函数名.类名等标识符. 快速查看关键字的方法除了上cs ...

  8. python提供的内置函数有哪些_python内置函数介绍

    内置函数,一般都是因为使用频率比较频繁,所以通过内置函数的形式提供出来.对内置函数通过分类分析,基本的数据操作有数学运算.逻辑操作.集合操作.字符串操作等. 说起我正式了解内置函数之前,接触到的是la ...

  9. python中如何调用函数_如何调用python中的内置函数?(实例解析)

    对于第一次接触到python这门编程语言的朋友来说,刚刚开始学习python编程的时候对于python函数调用这一方面的了解比较少,在这篇文章之中我们就来了解一下python怎么调用函数. Pytho ...

最新文章

  1. Page.OnPreInit 方法
  2. 需求分析挑战之旅(疯狂的订餐系统)(8)——最后的疯狂
  3. C语言 · 未名湖边的烦恼
  4. html中选择省份城市,省份、城市、区县三级联动Html代码
  5. Windows Mobile下使用Native C++开发日志类
  6. 解决Python shell中Delete-Backspace键乱码问题
  7. java底层 文件操作,java底层是怎的对文件操作的
  8. sql 存储过程分页
  9. Android studio3.2学习开发JNI并且生成so库教程
  10. 说你呢,装着JDK8,却孜孜不倦的写着 JDK6 的代码,写了3年了,JDK8的特性都没用过......
  11. Sequelize-nodejs-5-Querying
  12. Flash MX 认证考试(样题)
  13. 如何在通达信软件上随意画图_通达信指标公式编写教程:绘图函数DRAWLINE、DRAWTEXT 等...
  14. Linux下通过iwconfig命令连接无线
  15. 微软 Windows 10 Version 2004 新功能盘点:分离Cortana,数项体验升级,抢先体验
  16. 磊科路由器信号按键_磊科路由器信号增强怎么设置方法
  17. 威斯康星大学麦迪逊计算机本科,威斯康星大学麦迪逊分校计算机专业为什么火?申请要求及学费详解!...
  18. (十四)STM32——外部中断(EXTI)
  19. 浅谈软件测试行业的前景,就业方向和薪资待遇
  20. nessus安装及使用

热门文章

  1. 计算机原理课程代码二三八四,计算机原理(2012年版)课程代码:02384
  2. 信用卡号校验java_ES reduce 一行代码解决信用卡号验证问题
  3. java murmurhash实现_一致性哈希算法与Java实现
  4. 箱体图_靓爆了!东莞近千个市政箱体换上“新装”成街头风景线
  5. java+icepdf+下载_Java使用icepdf将pdf文件按页转成图片
  6. qwidget show 是否有信号_PyQt5信号与槽机制入门(一)
  7. 学习笔记(01):MySQL数据库运维与管理-02-设置系统变量
  8. 基于JAVA+SpringBoot+Mybatis+MYSQL的运动会管理系统
  9. 基于JAVA+Servlet+JSP+MYSQL的会议管理系统
  10. oracle监听的动态注册和静态注册