帮助

简介: 一、注释 确保对模块, 函数, 方法和行内注释使用正确的风格单行注释以 # 开头# 这是一个注释print("Hello, World!") 单引号(''')#!/usr/bin/python3 '''这是多行注释,用三个单引号这是多行注释,用三个单引号 这是多行注释,用三...

一、注释

确保对模块, 函数, 方法和行内注释使用正确的风格
  1. 单行注释以 # 开头
    # 这是一个注释
    print("Hello, World!") 
  2. 单引号(''')
    #!/usr/bin/python3 
    '''
    这是多行注释,用三个单引号
    这是多行注释,用三个单引号 
    这是多行注释,用三个单引号
    '''
    print("Hello, World!") 
  3. 双引号(""")
    #!/usr/bin/python3 
    """
    这是多行注释,用三个单引号
    这是多行注释,用三个单引号 
    这是多行注释,用三个单引号
    """
    print("Hello, World!") 

二、DIR

  • 语法:dir([object])
  • 说明:
    • 当不传参数时,返回当前作用域内的变量、方法和定义的类型列表。
      >>> dir()
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
      >>> a = 10 #定义变量a
      >>> dir() #多了一个a
      ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']
    • 当参数对象是模块时,返回模块的属性、方法列表。
      >>> import math
      >>> math
      <module 'math' (built-in)>
      >>> dir(math)
      ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
    • 当参数对象是类时,返回类及其子类的属性、方法列表。
      >>> class A:
          name = 'class'
      
      >>> a = A()
      >>> dir(a) #name是类A的属性,其他则是默认继承的object的属性、方法
      ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
    • 当对象定义了__dir__方法,则返回__dir__方法的结果
      >>> class B:
          def __dir__(self):
              return ['name','age']
      
      >>> b = B()
      >>> dir(b) #调用 __dir__方法
      ['age', 'name']

三、__doc__

将文档写在程序里,是LISP中的一个特色,Python也借鉴过。每个函数都是一个对象,每个函数对象都是有一个__doc__的属性,函数语句中,如果第一个表达式是一个string,这个函数的__doc__就是这个string,否则__doc__是None。
>>> def testfun():
"""
this function do nothing , just demostrate the use of the doc string .
"""
pass
>>> testfun.__doc__
'\nthis function do nothing , just demostrate the use of the doc string .\n'
>>> #pass 语句是空语句,什么也不干,就像C语言中的{} , 通过显示__doc__,我们可以查看一些内部函数的帮助信息
>>> " ".join.__doc__
'S.join(iterable) -> str\n\nReturn a string which is the concatenation of the strings in the\niterable. The separator between elements is S.'
>>>

四、help

  • 语法:help([object])
  • 说明:
    • 在解释器交互界面,不传参数调用函数时,将激活内置的帮助系统,并进入帮助系统。在帮助系统内部输入模块、类、函数等名称时,将显示其使用说明,输入quit退出内置帮助系统,并返回交互界面。
      >>> help() #不带参数
      
      Welcome to Python 3.5'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.5/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> str #str的帮助信息
      Help on class str in module builtins:
      
      class str(object)
      |  str(object='') -> str
      |  str(bytes_or_buffer[, encoding[, errors]]) -> str
      |  
      |  Create a new string object from the given object. If encoding or
      |  errors is specified, then the object must expose a data buffer
      |  that will be decoded using the given encoding and error handler.
      |  Otherwise, returns the result of object.__str__() (if defined)
      |  or repr(object).
      |  encoding defaults to sys.getdefaultencoding().
      |  errors defaults to 'strict'.
      |  
      |  Methods defined here:
      |  
      |  __add__(self, value, /)
      |      Return self+value.
      ................................
      
      
      help> 1 #不存在的模块名、类名、函数名
      No Python documentation found for '1'.
      Use help() to get the interactive help utility.
      Use help(str) for help on the str class.
      
      help> quit #退出内置帮助系统
      
      You are now leaving help and returning to the Python interpreter.
      If you want to ask for help on a particular object directly from the
      interpreter, you can type "help(object)".  Executing "help('string')"
      has the same effect as typing a particular string at the help> prompt.
      
      # 已退出内置帮助系统,返回交互界面 help> 变成 >>>
    • 在解释器交互界面,传入参数调用函数时,将查找参数是否是模块名、类名、函数名,如果是将显示其使用说明。
      >>> help(str) 
      Help on class str in module builtins:
      
      class str(object)
      |  str(object='') -> str
      |  str(bytes_or_buffer[, encoding[, errors]]) -> str
      |  
      |  Create a new string object from the given object. If encoding or
      |  errors is specified, then the object must expose a data buffer
      |  that will be decoded using the given encoding and error handler.
      |  Otherwise, returns the result of object.__str__() (if defined)
      |  or repr(object).
      |  encoding defaults to sys.getdefaultencoding().
      |  errors defaults to 'strict'.
      |  
      |  Methods defined here:
      |  
      |  __add__(self, value, /)
      |      Return self+value.
      |  
        ***************************

五、标准手册集

www.python.org
目录
相关文章
|
机器学习/深度学习 数据采集 测试技术
Toad:基于 Python 的标准化评分卡模型(上)
在信贷的风控模型中最常用、最经典的可能要属评分卡了,所谓评分卡就是给信贷客户进行打分,按照不同业务场景可为贷前、贷中、贷后和反欺诈,一般叫做ABCF卡。模型得到分数,通过设置cutoff阈值给出评估结果,结果可直接用于通过或拒绝,或者用于策略应用。
2453 0
Toad:基于 Python 的标准化评分卡模型(上)
|
算法 Linux
linux命令之xz
linux命令之xz
542 1
|
11月前
|
设计模式 网络协议 Java
10.桥接模式设计思想
本文介绍了桥接模式的设计思想和实现方法。桥接模式通过将抽象部分与实现部分分离,使它们可以独立变化,解决了多层继承带来的复杂性和耦合性问题。文章详细讲解了桥接模式的由来、定义、应用场景和实现步骤,并通过具体实例演示了如何在支付场景中使用桥接模式。此外,还讨论了桥接模式的优缺点及其适用环境,提供了丰富的代码示例和进一步学习的资源链接。
310 2
|
机器学习/深度学习 人工智能 并行计算
AIGC生成3D模型探索与实践
AIGC生成3D模型探索与实践
2148 1
|
XML SQL Java
IntelliJ IDEA 插件 MybatisX 在mapper和xml间跳转
IntelliJ IDEA 插件 MybatisX 在mapper和xml间跳转
3553 0
|
传感器 搜索推荐 机器人
具身智能赋能人形机器人产业将蓬勃发展
【1月更文挑战第12天】具身智能赋能人形机器人产业将蓬勃发展
292 3
具身智能赋能人形机器人产业将蓬勃发展
|
Ubuntu 前端开发 Shell
Linux apt命令详解
1.apt简介 apt(Advanced Packaging Tool)是一个在 Debian 和 Ubuntu 中的 Shell 前端软件包管理器。 apt 命令提供了查找、安装、升级、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。 apt 命令执行需要超级管理员权限(root)。
976 0
|
Java 数据安全/隐私保护 C++
bugly崩溃排查2:luajit编译调试
bugly崩溃排查2:luajit编译调试
351 0
|
数据中心 容器
容器与虚拟机的区别:以Web应用部署为例
容器与虚拟机的区别:以Web应用部署为例
368 0
|
人工智能 自然语言处理 达摩院
EasyNLP开源|中文NLP+大模型落地,EasyNLP is all you need
EasyNLP背后的技术框架如何设计?未来有哪些规划?今天一起来深入了解。
EasyNLP开源|中文NLP+大模型落地,EasyNLP is all you need