python自建模块显示说明与详情

通常我们自建模块放在/usr/lib/python2.7/site-packages下面,这样可以python就可以进行调用。

但是,自建模块也要有详细的说明情况,例如查help,可以看出来模块的作用。

例如:getpass模块help

  1 >>> help(getpass)
  2 Help on module getpass:
  3 
  4 NAME
  5     getpass - Utilities to get a password and/or the current user name.
  6 
  7 FILE
  8     /usr/lib64/python2.7/getpass.py
  9 
 10 DESCRIPTION
 11     getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
 12     getuser() - Get the user name from the environment or password database.
 13     
 14     GetPassWarning - This UserWarning is issued when getpass() cannot prevent
 15                      echoing of the password contents while reading.
 16     
 17     On Windows, the msvcrt module will be used.
 18     On the Mac EasyDialogs.AskPassword is used, if available.
 19 
 20 CLASSES
 21     exceptions.UserWarning(exceptions.Warning)
 22         GetPassWarning
 23     
 24     class GetPassWarning(exceptions.UserWarning)
 25      |  Method resolution order:
 26      |      GetPassWarning
 27      |      exceptions.UserWarning
 28      |      exceptions.Warning
 29      |      exceptions.Exception
 30      |      exceptions.BaseException
 31      |      __builtin__.object
 32      |  
 33      |  Data descriptors defined here:
 34      |  
 35      |  __weakref__
 36      |      list of weak references to the object (if defined)
 37      |  
 38      |  ----------------------------------------------------------------------
 39      |  Methods inherited from exceptions.UserWarning:
 40      |  
 41      |  __init__(...)
 42      |      x.__init__(...) initializes x; see help(type(x)) for signature
 43      |  
 44      |  ----------------------------------------------------------------------
 45      |  Data and other attributes inherited from exceptions.UserWarning:
 46      |  
 47      |  __new__ = <built-in method __new__ of type object>
 48      |      T.__new__(S, ...) -> a new object with type S, a subtype of T
 49      |  
 50      |  ----------------------------------------------------------------------
 51      |  Methods inherited from exceptions.BaseException:
 52      |  
 53      |  __delattr__(...)
 54      |      x.__delattr__('name') <==> del x.name
 55      |  
 56      |  __getattribute__(...)
 57      |      x.__getattribute__('name') <==> x.name
 58      |  
 59      |  __getitem__(...)
 60      |      x.__getitem__(y) <==> x[y]
 61      |  
 62      |  __getslice__(...)
 63      |      x.__getslice__(i, j) <==> x[i:j]
 64      |      
 65      |      Use of negative indices is not supported.
 66      |  
 67      |  __reduce__(...)
 68      |  
 69      |  __repr__(...)
 70      |      x.__repr__() <==> repr(x)
 71      |  
 72      |  __setattr__(...)
 73      |      x.__setattr__('name', value) <==> x.name = value
 74      |  
 75      |  __setstate__(...)
 76      |  
 77      |  __str__(...)
 78      |      x.__str__() <==> str(x)
 79      |  
 80      |  __unicode__(...)
 81      |  
 82      |  ----------------------------------------------------------------------
 83      |  Data descriptors inherited from exceptions.BaseException:
 84      |  
 85      |  __dict__
 86      |  
 87      |  args
 88      |  
 89      |  message
 90 
 91 FUNCTIONS
 92     getpass = unix_getpass(prompt='Password: ', stream=None)
 93         Prompt for a password, with echo turned off.
 94         
 95         Args:
 96           prompt: Written on stream to ask for the input.  Default: 'Password: '
 97           stream: A writable file object to display the prompt.  Defaults to
 98                   the tty.  If no tty is available defaults to sys.stderr.
 99         Returns:
100           The seKr3t input.
101         Raises:
102           EOFError: If our input tty or stdin was closed.
103           GetPassWarning: When we were unable to turn echo off on the input.
104         
105         Always restores terminal settings before returning.
106     
107     getuser()
108         Get the username from the environment or password database.
109         
110         First try various environment variables, then the password
111         database.  This works on Windows as long as USERNAME is set.

里面显示有NAME、FILE、DESCRIPTION、FUNCTIONS等。

那么自建模块中如保显示并且添加上述内容

添加方法如下:一个简单例子

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 
 4 """ 示例程序
 5 
 6 仅仅是一个测试,只有一个例子
 7 """
 8 
 9 def pstar():
10     "用于打印20个星号"
11     print('*' * 20)

然后,调用此模块,使用help查看帮助。

>>> import star
>>> help(star)
Help on module star:

NAME
    star - 示例程序

FILE
    /root/PycharmProjects/sandy/day1/star.py

DESCRIPTION
    仅仅是一个测试,只有一个例子

FUNCTIONS
    pstar()
        用于打印20个星号

这样的操作之后,可以使模块更加的规范。

 

posted @ 2018-07-04 09:00  启云  阅读(280)  评论(0编辑  收藏  举报