1. 程式人生 > >python中3個幫助函式help,dir,type的使用

python中3個幫助函式help,dir,type的使用

1 help函式:檢視模組、函式、變數的詳細說明:

  • 檢視模組
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

BaseHTTPServer      array               htmllib             sets
Bastion             ast                 httplib             sgmllib
CDROM               asynchat            ihooks              sha
CGIHTTPServer       asyncore            imaplib             shelve
Canvas              atexit              imghdr              shlex
ConfigParser        audiodev            imp                 shutil
Cookie              audioop             importlib           signal
DLFCN               axi                 imputil             site
Dialog              base64              inspect             sitecustomize
DocXMLRPCServer     bdb                 io                  smtpd
FileDialog          binascii            itertools           smtplib


  • 檢視包
>>> help("json")
Help on package json:

NAME
    json

FILE
    /usr/lib/python2.7/json/__init__.py

MODULE DOCS
    http://docs.python.org/library/json

DESCRIPTION
    JSON (JavaScript Object Notation) <http://json.org> is a subset of
    JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
    interchange format.
    
    :mod:`json` exposes an API familiar to users of the standard library
    :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
    version of the :mod:`json` library contained in Python 2.6, but maintains
    compatibility with Python 2.4 and Python 2.5 and (currently) has
    significant performance advantages, even without using the optional C
    extension for speedups.
    
    Encoding basic Python object hierarchies::


  • 檢視類
>>> help(json.JSONDecoder)
Help on class JSONDecoder in module json.decoder:

class JSONDecoder(__builtin__.object)
 |  Simple JSON <http://json.org> decoder
 |  
 |  Performs the following translations in decoding by default:
 |  
 |  +---------------+-------------------+
 |  | JSON          | Python            |
 |  +===============+===================+
 |  | object        | dict              |
 |  +---------------+-------------------+
 |  | array         | list              |
 |  +---------------+-------------------+
 |  | string        | unicode           |
 |  +---------------+-------------------+
 |  | number (int)  | int, long         |
 |  +---------------+-------------------+
 |  | number (real) | float             |
 |  +---------------+-------------------+
 |  | true          | True              |
 |  +---------------+-------------------+
 |  | false         | False             |


  • 檢視函式:
>>> help(json.dump)
Help on function dump in module json:

dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, **kw)
    Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
    ``.write()``-supporting file-like object).
    
    If ``skipkeys`` is true then ``dict`` keys that are not basic types
    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
    will be skipped instead of raising a ``TypeError``.
    
    If ``ensure_ascii`` is false, then the some chunks written to ``fp``
    may be ``unicode`` instances, subject to normal Python ``str`` to
    ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
    to cause an error.


2 dir函式:檢視變數可用的函式或方法

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'exitfunc', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']


3 type函式:檢視變數的型別

<type 'module'>
>>> type (json.__name__)
<type 'str'>
>>> type (json.decoder)
<type 'module'>

4 退出python命令列

windows: ctrl+z 回車

linux:ctrl+d 

注:使用pydoc module 可檢視模組的文件說明

相關推薦

python3幫助函式helpdirtype的使用

1 help函式:檢視模組、函式、變數的詳細說明: 檢視模組 >>> help("modules") Please wait a moment while I gather a list of all available modules... B

Python 3常用的資料結構和演算法

Python內建了許多非常有用的資料結構,比如列表(list)、集合(set)以及字典(dictionary)。就絕大部分情況而言

python的分支和迴圈:for 迴圈while迴圈三元操作符斷言assert關鍵字rang()函式總結

1.python中的條件語句 例:score=int(input('請輸入一個分數'))        if 100>=score>=90:              

Python非常神奇的程式碼的簡潔方便高效!

  我一直說python是非常優美的語言,那到底如何個美呢,其中有一個特性就是簡潔.很多果粉為啥特別喜歡蘋果手機和蘋果電腦,一方面確實做的漂亮,另外一個就是簡潔。你想蘋果手機開機關機,刪軟體都是一個步驟,而我們用win,關機至少3-5步,刪軟體更不要說了,非常麻煩. 有的時候

pythonmap()函式使用資料型別轉換

python中map()函式進行資料轉換 用法: map(function, iterable, …), 返回的是map型,(ps:python2中返回的是list型可以直接顯示,但在python3中是map型無法直接顯示) 引數function: 傳的是一

python編寫氣泡排序函式可以排序任意型別的元素可以逆序

1.實現氣泡排序演算法2.可以排序任意型別的元素3.能夠通過引數設定進行逆序,預設升序用for in 遍歷元素和if else處理。直接排序,根據鍵排序兩種情況,各自又分為升序和降序兩種情況排序。def list_sort(lt, key=None, reverse=Fals

pythonscatter()函式用法matplotlib畫圖

scatter函式用於繪製散點圖。 scatter函式原型 matplotlib.pyplot.scatter(x,y,s=20,c='b',maker='o',cmpa=None,norm=None,vmin=None,vax=None,alpha=None,linewi

python練習:編寫一個程序檢查3變量x,y,z輸出其中最大的奇數。如果其中沒有奇數就輸出一個消息進行說明。

int elif pri 檢查 說明 一個數 print 下一個 == python練習:編寫一個程序,檢查3個變量x,y,z,輸出其中最大的奇數。如果其中沒有奇數,就輸出一個消息進行說明。 筆者是只使用條件語句實行的。(if-else) 重難點:先把三個數進行由小到大的排

intelliJ IDEA (JetBrains PyCharm) 3地方設定字型大小文字編輯的字型大小介面字型大小顯示log的字型大小

在使用這個intelliJ IDEA (JetBrains PyCharm)編輯器的時候,可能剛剛開始要設定合適的字型大小,但是除了設定,編輯程式碼時的文字的字型大小外。 在現實console,就是那個log框的字型也是很小。這個地方的設定,也不是一下兩下就能找到的。

Python 如何獲取當前位置所在的檔名函式以及行號

在C/C++中可以分別使用 __FILE__ , __FUNCTION__ , __LINE__ 來得到當前位置所在的檔名,函式名,行號 在Python中可以通過模組sys中的函式來獲得當前位置所在的檔名,函式名,以及行號 import sys def function(

pythonmatplotlib使用雙y軸的時候只能顯示第二曲線標誌怎麼辦?

       使用雙y軸的時候,只能顯示第二個曲線標誌怎麼辦? 原因:        只顯示右邊一個label,是因為在windows中legend()函式只選取當前活動的ax2。 方法:         在第二個Y 軸之前,即可用ax1.legend()語句把當前軸的標

python3位數的水仙花數和5位數回文數的個數

and mce del tro 位數 ron 個數 size str 3位數中的水仙花數打印num=100 e=0while num<1000: b=num%10 c=num//10%10 d=num//100 if b**3+c**3+d**

尋找數組3和為0的所有數字組合要求不能重復(3 sum)

pen 數字組合 class iuc def i+1 += out art 示例: 輸入:[-2,3,-1,1,-1,2] 輸出:[[-2,-1,3],[-1,-1,2]] Python解決方案1: 固定其中一個數,對另外兩個數進行考察 class Solut

python68內建函式的總結

內建函式 內建函式就是python給你提供的, 拿來直接用的函式, 比如print., input等. 截止到python版本3.6.2 python一共提供了68個內建函式. #68個內建函式 # abs()   dict()   help()   min()

還在用百度找資源?試試這3頂級資源搜索網站沒有找不到的!

搜狗 網盤 索引 ref 搜索網站 進行 sogou 選擇 ogl 資源搜索是工作和學習的日常,相信很多人都喜歡用百度去搜索,雖然百度很強大,但是畢竟資源有限,今天再給大家分享3個頂級資源搜索網站,視頻、音樂、圖片等應有盡有,沒有你找不到的哦。 1.蟲部落 一個解決稀缺資源

python的turtle庫函式簡單使用

      參考案例: import turtle d=0 for i in range(4): turtle.fd(200) #或者寫成turtle.forward(200) d =d+90

pythonQPushButton響應同一個事件

python中多個QPushButton響應同一個事件 注:在python2 的環境下執行 #! -*- coding:utf-8 -*- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayou

解釋pythonjoin()和split()函式

join能讓我們將指定字元新增至字串中 a=','.join('123456') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'>   split()能讓我們用指定字元分割字串 a='1,2,3,4

python一些常用的函式(不定時更新)

一、random函式 import random # 在1~20中隨機取一個數 print(random.choice(range(1, 20))) 9 # 在1~20中隨機取五個陣列成一個列表 print(random.choices(range(1, 20), k=5)) [3, 1