python中dis的用法

dis庫(kù)是python(默認(rèn)的CPython)自帶的一個(gè)庫(kù),可以用來(lái)分析字節(jié)碼

例子

首先導(dǎo)入dis庫(kù)

>>> import dis

然后在repl中,創(chuàng)建一個(gè)函數(shù)

>>> def add(a, b = 0):
...     return a + b
... 
>>> 

最后將add函數(shù)傳給dis庫(kù)的dis函數(shù)

>>> dis.dis(add)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 BINARY_ADD
              6 RETURN_VALUE
>>> 

repl會(huì)返回add函數(shù)的字節(jié)碼.

分析

來(lái)看看dis函數(shù)的源碼

def dis(x=None, *, file=None):
    """Disassemble classes, methods, functions, generators, or code.
    With no argument, disassemble the last traceback.
    """
    if x is None:
        distb(file=file)
        return
    if hasattr(x, '__func__'):  # Method
        x = x.__func__
    if hasattr(x, '__code__'):  # Function
        x = x.__code__
    if hasattr(x, 'gi_code'):  # Generator
        x = x.gi_code
    if hasattr(x, '__dict__'):  # Class or module
        items = sorted(x.__dict__.items())
        for name, x1 in items:
            if isinstance(x1, _have_code):
                print("Disassembly of %s:" % name, file=file)
                try:
                    dis(x1, file=file)
                except TypeError as msg:
                    print("Sorry:", msg, file=file)
                print(file=file)
    elif hasattr(x, 'co_code'): # Code object
        disassemble(x, file=file)
    elif isinstance(x, (bytes, bytearray)): # Raw bytecode
        _disassemble_bytes(x, file=file)
    elif isinstance(x, str):    # Source code
        _disassemble_str(x, file=file)
    else:
        raise TypeError("don't know how to disassemble %s objects" %
                        type(x).__name__)

x參數(shù)可以是None、Method、Function、Generator、Class、module、Code object、Raw bytecode、Source code,如果x是Method、Function、Generator,只用返回對(duì)應(yīng)的字節(jié)碼,
如果x是Class或者module,那會(huì)返回x的所有元素(先排序)的字節(jié)碼,這一句代碼x.dict.items()有提現(xiàn).如果x是Code object或者Raw bytecode,或者Source code,那么會(huì)調(diào)用對(duì)應(yīng)的disassemble函數(shù). disassemble函數(shù)是干嘛的呢,顧名思義,就是assemble的反義詞, assemble是匯編的意思,那disassemble自然是有一個(gè) 反匯編 的意思,當(dāng)然這里并不是真的反匯編,而是只是輸出字節(jié)碼.

disassemble的函數(shù)細(xì)節(jié)可以自己去看看源碼,源碼也在dis.py里

源碼

https://github.com/python/cpython/blob/3.6/Lib/dis.py

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 編程派微信號(hào):codingpy 本文約有 15000 字,讀完可能需要 20 分鐘。原文地址: 500lines。...
    羅義的夏天閱讀 65,136評(píng)論 0 22
  • 本節(jié)內(nèi)容 Python介紹 發(fā)展史 Python 2 or 3? 安裝 Hello World程序 變量 用戶輸入...
    小小不懂11閱讀 3,548評(píng)論 2 30
  • 放著我的旅行日記沒(méi)寫,倒是跑來(lái)先寫這些亂七八糟的東西了。一個(gè)假期都在反反復(fù)復(fù)想著始終在心里的事情,得到的答案卻還是...
    阿喂閱讀 603評(píng)論 0 2
  • 如果說(shuō)生氣是自找的,很多人都不以為然。生氣的源頭,往往是對(duì)方的意圖與自己的意念發(fā)生沖突,最終沒(méi)有按自己的意圖...
    我愛語(yǔ)雯閱讀 250評(píng)論 0 2
  • 都說(shuō)金牛座好吃但不懶做,這是一個(gè)星座界公認(rèn)的說(shuō)法。 對(duì)于“不懶做”,不可置否,從大學(xué)工業(yè)自動(dòng)化專業(yè)畢業(yè)后,我誤打誤...
    斯諾史蒂芬周閱讀 467評(píng)論 0 0

友情鏈接更多精彩內(nèi)容