python?函數定位參數+關鍵字參數+inspect模塊
更新時間:2022年05月13日 11:46:57 作者:wx59129d39de499
這篇文章主要介紹了python?函數定位參數+關鍵字參數+inspect模塊,文章圍繞主題展開詳細的相關資料,具有一定的參考價值,需要的小伙伴可以參考一下
函數內省(function introspection)
除了__doc__屬性, 函數對象還有很多屬性,對于下面的函數,可以使用dir()查看函數具有的屬性:
>>> dir(factorial) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']?
其中大多數是Python常規(guī)類都有的屬性,下面重點看看常規(guī)對象沒有而函數對象有的屬性:
>>> class C:pass ... >>> obj = C() >>> def func():pass ... >>> sorted(set(dir(func)) - set(dir(obj))) # 計算差集,然后排序 ['__annotations__', '__call__', '__closure__', '__code__', '__defaults__', '__get__', '__globals__', '__kwdefaults__', '__name__', '__qualname__']
對于上面列出的函數特有屬性,說明如下:
- __annotations__ dict 參數和返回值的注釋
- __call__ method-wrapper 實現()運算符,即可調用對象的協(xié)議
- __closure__ tuple 函數閉包,即自由變量的綁定(通常是None)
- __code__ code 編譯成字節(jié)碼的函數元數據和函數定義體
- __defaults__ tuple 形式參數的默認值
- __get__ method-wrapper 實現只讀描述符協(xié)議
- __globals__ dict 函數所在的模塊中的全局變量
- __kwdefaults__ dict 僅限關鍵字形式參數的默認值
- __name__ str 函數名稱
- __qualname__ str 函數的限定名稱
定位參數和僅限關鍵字參數
def tag(name,*content,cls=None,**attrs): if cls is not None: attrs['class'] = cls if attrs: attrs_str = ''.join(' %s="%s" ' % (attr,value) for attr,value in sorted(attrs.items())) else: attrs_str='' if content: return '\n'.join('<%s %s >%s</%s>' % (name,attrs_str,c,name) for c in content) else: return '<%s%s />' % (name,attrs_str) print(tag('br'))#定位參數 name print(tag('p','hello'))#hello 會被*conteng捕獲 存入元組content = ('hello') print(tag('p','hello','world'))#content = ('hello','world') print(tag('p','hello',id=33)) #attrs={'id':33} content = ('hello') print(tag('p','hello','world',cls='sidebar'))#cls 關鍵字傳入 cls='sidebar' print(tag(content='testing',name='img'))#第一個參數name 也能作為關鍵字傳入 #同名鍵會綁定到對應的具名參數上,剩余的則會被**attrs捕獲 print(tag(**{'name':'img','title':'sunset boulevard','src':'sunset.jpg','cls':'framed'})) #僅限關鍵字參數是python3.0新增的特性,在上例中,cls參數只能通過關鍵字參數指定,他一定不會捕獲未命名的定位參數 #定義函數時候,如果想指定僅限關鍵字參數,要把它們放到*的參數后面 def f(a,*,b): return a,b ff = f(1,b=2) print(ff)
<br /> <p >hello</p> <p >hello</p> <p >world</p> <p id="33" >hello</p> <p class="sidebar" >hello</p> <p class="sidebar" >world</p> <img content="testing" /> <img class="framed" src="sunset.jpg" title="sunset boulevard" /> (1, 2)
inspect模板
def tag(name,*content,cls=None,**attrs): if cls is not None: attrs['class'] = cls if attrs: attrs_str = ''.join(' %s="%s" ' % (attr,value) for attr,value in sorted(attrs.items())) else: attrs_str='' if content: return '\n'.join('<%s %s >%s</%s>' % (name,attrs_str,c,name) for c in content) else: return '<%s%s />' % (name,attrs_str) import inspect sig = inspect.signature(tag) print(sig) my_tag = {'name':'img','title':'sun long','src':'sunlong.jpg','cls':'framed'} bound_args = sig.bind(**my_tag) for name,value in bound_args.arguments.items(): print(name,'=',value) print(bound_args)
inspect模塊把實參綁定給函數調用:
(name, *content, cls=None, **attrs) name = img cls = framed attrs = {'title': 'sun long', 'src': 'sunlong.jpg'} <BoundArguments (name='img', cls='framed', attrs={'title': 'sun long', 'src': 'sunlong.jpg'})>
到此這篇關于python 函數定位參數+關鍵字參數+inspect模塊的文章就介紹到這了,更多相關python inspect模塊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!