python的inspect模組

來源:互聯網
上載者:User

標籤:產生器   isa   cal   expr   cat   調用   ota   Fix   ati   

一、type and members

1. 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等)的如下屬性: 

Type Attribute Description Notes
module __doc__ documentation string  
  __file__ filename (missing for built-in modules)  
class __doc__ documentation string  
  __module__ name of module in which this class was defined  
method __doc__ documentation string  
  __name__ name with which this method was defined  
  im_class class object that asked for this method (1)
  im_func or __func__ function object containing implementation of method  
  im_self or __self__ instance to which this method is bound, or None  
function __doc__ documentation string  
  __name__ name with which this function was defined  
  func_code code object containing compiled function bytecode  
  func_defaults tuple of any default values for arguments  
  func_doc (same as __doc__)  
  func_globals global namespace in which this function was defined  
  func_name (same as __name__)  
generator __iter__ defined to support iteration over container  
  close raises new GeneratorExit exception inside the generator to terminate the iteration  
  gi_code code object  
  gi_frame frame object or possibly None once the generator has been exhausted  
  gi_running set to 1 when generator is executing, 0 otherwise  
  next return the next item from the container  
  send resumes the generator and “sends” a value that becomes the result of the current yield-expression  
  throw used to raise an exception inside the generator  
traceback tb_frame frame object at this level  
  tb_lasti index of last attempted instruction in bytecode  
  tb_lineno current line number in Python source code  
  tb_next next inner traceback object (called by this level)  
frame f_back next outer frame object (this frame’s caller)  
  f_builtins builtins namespace seen by this frame  
  f_code code object being executed in this frame  
  f_exc_traceback traceback if raised in this frame, or None  
  f_exc_type exception type if raised in this frame, or None  
  f_exc_value exception value if raised in this frame, or None  
  f_globals global namespace seen by this frame  
  f_lasti index of last attempted instruction in bytecode  
  f_lineno current line number in Python source code  
  f_locals local namespace seen by this frame  
  f_restricted 0 or 1 if frame is in restricted execution mode  
  f_trace tracing function for this frame, or None  
code co_argcount number of arguments (not including * or ** args)  
  co_code string of raw compiled bytecode  
  co_consts tuple of constants used in the bytecode  
  co_filename name of file in which this code object was created  
  co_firstlineno number of first line in Python source code  
  co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg |8=**arg  
  co_lnotab encoded mapping of line numbers to bytecode indices  
  co_name name with which this code object was defined  
  co_names tuple of names of local variables  
  co_nlocals number of local variables  
  co_stacksize virtual machine stack space required  
  co_varnames tuple of names of arguments and local variables  
builtin __doc__ documentation string  
  __name__ original name of this function or method  
  __self__ instance to which a method is bound, or None  

 

2. inspect.getmoduleinfo(path): 返回一個命名元組<named tuple>(name, suffix, mode, module_type)

  name:模組名(不包括其所在的package)

      suffix:

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

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

3. inspect.getmodulename(path):根據path返回模組名(不包括其所在的package)

 

二、Retrieving source code

1. inspect.getdoc(object): 擷取object的documentation資訊

2. inspect.getcomments(object)

3. inspect.getfile(object): 返回對象的檔案名稱

4. inspect.getmodule(object):返回object所屬的模組名

5. inspect.getsourcefile(object): 返回object的python源檔案名稱;object不能使built-in的module, class, mothod

6. inspect.getsourcelines(object):返回object的python源檔案代碼的內容,行號+程式碼

7. inspect.getsource(object):以string形式返回object的原始碼

8. inspect.cleandoc(doc):

 

三、class and functions

1. inspect.getclasstree(classes[, unique])

2. inspect.getargspec(func)

3. inspect.getargvalues(frame)

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

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

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

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

>>> 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)Traceback (most recent call last):...TypeError: f() takes at least 1 argument (0 given)

四、The interpreter stack

1. inspect.getframeinfo(frame[, context])

2. inspect.getouterframes(frame[, context])

3. inspect.getinnerframes(traceback[, context])

4. inspect.currentframe()

5. inspect.stack([context])

6. inspect.trace([context])

轉自:45670805

python的inspect模組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.