Python標準庫inspect,python標準inspect

來源:互聯網
上載者:User

Python標準庫inspect,python標準inspect

inspect模組用於收集python對象的資訊,可以擷取類或函數的參數的資訊,源碼,解析堆棧,對對象進行類型檢查等等,有幾個好用的方法:

getargspec(func)

返回一個命名元組ArgSpect(args, varargs, keywords, defaults),args是函數位置參數名列表,varargs是*參數名,keywords是**參數名,defaults是預設參數值的元組。

在用__init__參數自動初始化執行個體屬性的實踐中,是用位元組碼對象的co_varnames屬性來擷取函數的位置參數名的:

def attr_from_locals(locals_dict):    self = locals_dict.pop('self')    code = self.__init__.__func__.__code__    args = code.co_varnames[1:code.co_argcount]    for k in args:        setattr(self, k, locals_dict[k])         class Foo(object):    def __init__(self, name, color, num=1):        x = 1        attr_from_locals(locals())

而當__init__方法使用**特殊參數接收任意數量的關鍵字參數時,上述代碼是不適用的。可行的辦法是使用位元組碼的co_flags屬性來判斷**參數是否存在。

函數使用*args文法來接受任意數量的位置參數時,co_flags置位0x04,使用**kwargs文法時,置位0x08,函數為一個產生器時,置位0x2000,其它位保留:

>>> def foo(x, *args, **kwargv):    pass>>> foo.__code__.co_varnames('x', 'args', 'kwargv')>>> foo.__code__.co_flags & 0x044>>> foo.__code__.co_flags & 0x088

inspect模組的getargspec()方法正是用此判斷來擷取函數的特殊參數的。現在可以方便的擷取__init__的**參數了:

import inspectdef attr_from_locals(locals_dict):    self = locals_dict.pop('self')    args = inspect.getargspec(self.__init__.__func__).args[1:]    for k in args:        setattr(self, k, locals_dict[k])    keywords = inspect.getargspec(self.__init__.__func__).keywords    if keywords:        keywords_dict = locals_dict[keywords]        for k in keywords_dict:            setattr(self, k, keywords_dict[k])       class Foo(object):    def __init__(self, name, **kwargv):        attr_from_locals(locals())f = Foo('bar', color='yellow', num=1)print f.__dict__

結果為:

{'color': 'yellow', 'num': 1, 'name': 'bar'}

對象已經正確的初始化了。

getmembers(object[, predicate])

返回一個包含對象的所有成員的(name, value)列表。返回的內容比對象的__dict__包含的內容多,源碼是通過dir()實現的。

predicate是一個可選的函數參數,被此函數判斷為True的成員才被返回。

getmodule(object)

返回定義對象的模組

getsource(object)

返回對象的原始碼

getsourcelines(object)

返回一個元組,元組第一項為對象原始碼行的列表,第二項是第一行原始碼的行號

ismodule,isclass,ismethod,isfunction,isbuiltin

一系列判斷物件類型的方法,大都是封裝了isinstance(object, types.FunctionType)之類語句的函數。

現在可以用類型判斷來返回一個類的方法了:

class Foo(object):    '''Foo doc'''    def __init__(self, name):        self.__name = name    def getname(self):        return self.__nameinspect.getmembers(Foo, inspect.ismethod)

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.