日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

inspect模块---检查活动对象

發布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 inspect模块---检查活动对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[inspect]模塊提供了一些有用的函數來幫助獲取有關活動對象(如模塊,類,方法,函數,跟蹤,框架對象和代碼對象)的信息。例如,它可以幫助您檢查類的內容,檢索方法的源代碼,提取和格式化函數的參數列表,或獲取顯示詳細追溯所需的所有信息。

這個模塊提供了四種主要的服務:

類型檢查,

獲取源代碼,

檢查類和函數,

以及檢查解釋器堆棧

一、type and members

  • inspect.``getmembers`(object[, predicate])
  • 第二個參數通常可以根據需要調用如下16個方法;

    返回值為object的所有成員,以(name,value)對組成的列表

    inspect.ismodule(object): 是否為模塊

    inspect.isclass(object):是否為類

    inspect.ismethod(object):是否為方法(bound method written in python)

    inspect.isfunction(object):是否為函數(python function, including lambda expression)

    inspect.isgeneratorfunction(object):是否為python生成器函數

    inspect.isgenerator(object):是否為生成器

    inspect.istraceback(object): 是否為traceback

    inspect.isframe(object):是否為frame

    inspect.iscode(object):是否為code

    inspect.isbuiltin(object):是否為built-in函數或built-in方法

    inspect.isroutine(object):是否為用戶自定義或者built-in函數或方法

    inspect.isabstract(object):是否為抽象基類

    inspect.ismethoddescriptor(object):是否為方法標識符

    inspect.isdatadescriptor(object):是否為數字標識符,數字標識符有__get__ 和__set__屬性; 通常也有__name__和__doc__屬性

    inspect.isgetsetdescriptor(object):是否為getset descriptor

    inspect.ismemberdescriptor(object):是否為member descriptor

    inspect的getmembers()方法可以獲取對象(module、class、method等)的如下屬性:

    TypeAttributeDescriptionNotes
    moduledocdocumentation string
    filefilename (missing for built-in modules)
    classdocdocumentation string
    modulename of module in which this class was defined
    methoddocdocumentation string
    namename with which this method was defined
    im_classclass object that asked for this method(1)
    im_func or funcfunction object containing implementation of method
    im_self or selfinstance to which this method is bound, or None
    functiondocdocumentation string
    namename with which this function was defined
    func_codecode object containing compiled function bytecode
    func_defaultstuple of any default values for arguments
    func_doc(same as doc)
    func_globalsglobal namespace in which this function was defined
    func_name(same as name)
    generatoriterdefined to support iteration over container
    closeraises new GeneratorExit exception inside the generator to terminate the iteration
    gi_codecode object
    gi_frameframe object or possibly None once the generator has been exhausted
    gi_runningset to 1 when generator is executing, 0 otherwise
    nextreturn the next item from the container
    sendresumes the generator and “sends” a value that becomes the result of the current yield-expression
    throwused to raise an exception inside the generator
    tracebacktb_frameframe object at this level
    tb_lastiindex of last attempted instruction in bytecode
    tb_linenocurrent line number in Python source code
    tb_nextnext inner traceback object (called by this level)
    framef_backnext outer frame object (this frame’s caller)
    f_builtinsbuiltins namespace seen by this frame
    f_codecode object being executed in this frame
    f_exc_tracebacktraceback if raised in this frame, or None
    f_exc_typeexception type if raised in this frame, or None
    f_exc_valueexception value if raised in this frame, or None
    f_globalsglobal namespace seen by this frame
    f_lastiindex of last attempted instruction in bytecode
    f_linenocurrent line number in Python source code
    f_localslocal namespace seen by this frame
    f_restricted0 or 1 if frame is in restricted execution mode
    f_tracetracing function for this frame, or None
    codeco_argcountnumber of arguments (not including * or ** args)
    co_codestring of raw compiled bytecode
    co_conststuple of constants used in the bytecode
    co_filenamename of file in which this code object was created
    co_firstlinenonumber of first line in Python source code
    co_flagsbitmap: 1=optimized | 2=newlocals | 4=*arg |8=**arg
    co_lnotabencoded mapping of line numbers to bytecode indices
    co_namename with which this code object was defined
    co_namestuple of names of local variables
    co_nlocalsnumber of local variables
    co_stacksizevirtual machine stack space required
    co_varnamestuple of names of arguments and local variables
    builtindocdocumentation string
    nameoriginal name of this function or method
    selfinstance to which a method is bound, or None
  • inspect.``getmoduleinf``o(path): 返回一個命名元組(name, suffix, mode, module_type)
  • name:模塊名(不包括其所在的package)

    ? suffix:

    ? mode:open()方法的模式,如:‘r’, 'a’等

    ? module_type: 整數,代表了模塊的類型

  • inspect.``getmodulename(path):根據path返回模塊名(不包括其所在的package)
  • 二、Retrieving source code

  • inspect.getdoc`(object): 獲取object的documentation信息

  • inspect.``getcomments`(object)

  • inspect.``getfile`(object): 返回對象的文件名

  • inspect.``getmodule(object):返回object所屬的模塊名

  • inspect.``getsourcefile(object): 返回object的python源文件名;object不能使built-in的module, class, mothod

  • inspect.``getsourcelines(object):返回object的python源文件代碼的內容,行號+代碼行

  • inspect.``getsource(object):以string形式返回object的源代碼

  • inspect.``cleandoc(doc):

  • 三、class and functions

  • inspect.``getclasstree(classes[, unique])

  • inspect.``getargspec(func)

  • inspect.``getargvalues(frame)

  • inspect.``formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join])

  • inspect.``formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join])

  • inspect.``getmro(cls): 元組形式返回cls類的基類(包括cls類),以method resolution順序;通常cls類為元素的第一個元素

  • inspect.``getcallargs`(func[, *args][, **kwds]):將args和kwds參數到綁定到為func的參數名;對bound方法,也綁定第一個參數(通常為self)到相應的實例;返回字典,對應參數名及其值;

  • ''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! '''>> from inspect import getcallargs>> def f(a, b=1, *pos, **named):.. pass>> getcallargs(f, 1, 2, 3)'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}>> getcallargs(f, a=2, x=4)'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}>> getcallargs(f)raceback (most recent call last):..ypeError: f() takes at least 1 argument (0 given)

    四、The interpreter stack

  • inspect.``getframeinfo(frame[, context])

  • inspect.``getouterframes(frame[, context])

  • inspect.``getinnerframes(traceback[, context])

  • inspect.``currentframe()

  • inspect.``stack([context])

  • inspect.``trace([context])

  • 今天看RYU源碼時,發現一個inspect模塊,RYU使用了該模塊的getmembers函數來獲取ryu app的app類。

    函數原型是 inspect.getmembers(object[, predicate])

    功能: 從一個Object中獲取符合predicate的元素的list,元素的形式是(name,value)
    predicate可以是ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
    isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),isroutine(),這些驗證函數。

    看一個例子

    ''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' import inspect class C():class CC():def foo3():print "foo3"def foo():print "foo"def foo2():print "foo2" cls = inspect.getmembers(C,inspect.ismethod) print cls

    總結

    以上是生活随笔為你收集整理的inspect模块---检查活动对象的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。