python自建模塊顯示說明與詳情

一般咱們自建模塊放在/usr/lib/python2.7/site-packages下面,這樣能夠python就能夠進行調用。python

可是,自建模塊也要有詳細的說明狀況,例如查help,能夠看出來模塊的做用。python2.7

例如: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等。ui

那麼自建模塊中如保顯示而且添加上述內容spa

添加方法以下:一個簡單例子unix

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 
 4 """ 示例程序
 5 
 6 僅僅是一個測試,只有一個例子
 7 """
 8 
 9 def pstar():
10     "用於打印20個星號"
11     print('*' * 20)

而後,調用此模塊,使用help查看幫助。rest

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

NAME
    star - 示例程序

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

DESCRIPTION
    僅僅是一個測試,只有一個例子

FUNCTIONS
    pstar()
        用於打印20個星號

這樣的操做以後,能夠使模塊更加的規範。code

相關文章
相關標籤/搜索