python?函數(shù)定位參數(shù)+關(guān)鍵字參數(shù)+inspect模塊
函數(shù)內(nèi)省(function introspection)
除了__doc__屬性, 函數(shù)對象還有很多屬性,對于下面的函數(shù),可以使用dir()查看函數(shù)具有的屬性:
>>> 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__']?
其中大多數(shù)是Python常規(guī)類都有的屬性,下面重點看看常規(guī)對象沒有而函數(shù)對象有的屬性:
>>> 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__']
對于上面列出的函數(shù)特有屬性,說明如下:
- __annotations__ dict 參數(shù)和返回值的注釋
- __call__ method-wrapper 實現(xiàn)()運算符,即可調(diào)用對象的協(xié)議
- __closure__ tuple 函數(shù)閉包,即自由變量的綁定(通常是None)
- __code__ code 編譯成字節(jié)碼的函數(shù)元數(shù)據(jù)和函數(shù)定義體
- __defaults__ tuple 形式參數(shù)的默認(rèn)值
- __get__ method-wrapper 實現(xiàn)只讀描述符協(xié)議
- __globals__ dict 函數(shù)所在的模塊中的全局變量
- __kwdefaults__ dict 僅限關(guān)鍵字形式參數(shù)的默認(rèn)值
- __name__ str 函數(shù)名稱
- __qualname__ str 函數(shù)的限定名稱
定位參數(shù)和僅限關(guān)鍵字參數(shù)
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'))#定位參數(shù) 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 關(guān)鍵字傳入 cls='sidebar'
print(tag(content='testing',name='img'))#第一個參數(shù)name 也能作為關(guān)鍵字傳入
#同名鍵會綁定到對應(yīng)的具名參數(shù)上,剩余的則會被**attrs捕獲
print(tag(**{'name':'img','title':'sunset boulevard','src':'sunset.jpg','cls':'framed'}))
#僅限關(guān)鍵字參數(shù)是python3.0新增的特性,在上例中,cls參數(shù)只能通過關(guān)鍵字參數(shù)指定,他一定不會捕獲未命名的定位參數(shù)
#定義函數(shù)時候,如果想指定僅限關(guān)鍵字參數(shù),要把它們放到*的參數(shù)后面
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模塊把實參綁定給函數(shù)調(diào)用:
(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'})>到此這篇關(guān)于python 函數(shù)定位參數(shù)+關(guān)鍵字參數(shù)+inspect模塊的文章就介紹到這了,更多相關(guān)python inspect模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opencv導(dǎo)入頭文件時報錯#include的解決方法
這篇文章主要介紹了opencv導(dǎo)入頭文件時報錯#include的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python多進(jìn)程與服務(wù)器并發(fā)原理及用法實例分析
這篇文章主要介紹了Python多進(jìn)程與服務(wù)器并發(fā)原理及用法,深入淺出的介紹了進(jìn)程、并行、并發(fā)、同步、異步等相關(guān)概念與原理,并結(jié)合實例形式給出了Python多進(jìn)程編程相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn)
這篇文章主要介紹了Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python PyQt5實現(xiàn)拖拽與剪貼板功能詳解
這篇文章主要為大家詳細(xì)介紹了Python PyQt5如何實現(xiàn)拖拽與剪貼板功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12

