欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python標(biāo)準(zhǔn)庫(kù)inspect的具體使用方法

 更新時(shí)間:2017年12月06日 09:17:38   作者:再見(jiàn)紫羅蘭  
本篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)inspect的具體使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

inspect模塊用于收集python對(duì)象的信息,可以獲取類或函數(shù)的參數(shù)的信息,源碼,解析堆棧,對(duì)對(duì)象進(jìn)行類型檢查等等,有幾個(gè)好用的方法:

Doc:這樣寫到

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.

這個(gè)模塊是針對(duì)模塊,類,方法,功能等對(duì)象提供些有用的方法。

getargspec(func)

返回一個(gè)命名元組ArgSpect(args, varargs, keywords, defaults),args是函數(shù)位置參數(shù)名列表,varargs是*參數(shù)名,keywords是**參數(shù)名,defaults是默認(rèn)參數(shù)值的元組。

在用__init__參數(shù)自動(dòng)初始化實(shí)例屬性的實(shí)踐中,是用字節(jié)碼對(duì)象的co_varnames屬性來(lái)獲取函數(shù)的位置參數(shù)名的:

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())

而當(dāng)__init__方法使用**特殊參數(shù)接收任意數(shù)量的關(guān)鍵字參數(shù)時(shí),上述代碼是不適用的??尚械霓k法是使用字節(jié)碼的co_flags屬性來(lái)判斷**參數(shù)是否存在。

函數(shù)使用*args語(yǔ)法來(lái)接受任意數(shù)量的位置參數(shù)時(shí),co_flags置位0x04,使用**kwargs語(yǔ)法時(shí),置位0x08,函數(shù)為一個(gè)生成器時(shí),置位0x2000,其它位保留:

>>> def foo(x, *args, **kwargv):
  pass
>>> foo.__code__.co_varnames
('x', 'args', 'kwargv')
>>> foo.__code__.co_flags & 0x04
4
>>> foo.__code__.co_flags & 0x08
8

inspect模塊的getargspec()方法正是用此判斷來(lái)獲取函數(shù)的特殊參數(shù)的?,F(xiàn)在可以方便的獲取__init__的**參數(shù)了:

import inspect
def 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__

結(jié)果為:

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

對(duì)象已經(jīng)正確的初始化了。

getmembers(object[, predicate])

返回一個(gè)包含對(duì)象的所有成員的(name, value)列表。返回的內(nèi)容比對(duì)象的__dict__包含的內(nèi)容多,源碼是通過(guò)dir()實(shí)現(xiàn)的。

predicate是一個(gè)可選的函數(shù)參數(shù),被此函數(shù)判斷為True的成員才被返回。

getmodule(object)

返回定義對(duì)象的模塊

getsource(object)

返回對(duì)象的源代碼

getsourcelines(object)

返回一個(gè)元組,元組第一項(xiàng)為對(duì)象源代碼行的列表,第二項(xiàng)是第一行源代碼的行號(hào)

ismodule,isclass,ismethod,isfunction,isbuiltin

一系列判斷對(duì)象類型的方法,大都是包裝了isinstance(object, types.FunctionType)之類語(yǔ)句的函數(shù)。

現(xiàn)在可以用類型判斷來(lái)返回一個(gè)類的方法了:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論