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

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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python代碼打包超詳細教程

    python代碼打包超詳細教程

    在Python開發(fā)的過程中我們經常會需要將自己的代碼打包成一個可執(zhí)行文件,方便將代碼分享給其他人使用,下面這篇文章主要給大家介紹了關于python代碼打包的相關資料,需要的朋友可以參考下
    2023-06-06
  • Python命令行運行文件的實例方法

    Python命令行運行文件的實例方法

    在本篇文章里小編給大家整理的是一篇關于Python命令行運行文件的實例方法,有興趣的朋友們可以學習參考下。
    2021-03-03
  • opencv導入頭文件時報錯#include的解決方法

    opencv導入頭文件時報錯#include的解決方法

    這篇文章主要介紹了opencv導入頭文件時報錯#include的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • wxpython繪制圓角窗體

    wxpython繪制圓角窗體

    這篇文章主要為大家詳細介紹了wxpython繪制圓角窗體,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python中的json內置庫詳解

    Python中的json內置庫詳解

    這篇文章主要介紹了Python中的json內置庫詳解,在學習做自動化測試的過程中,python 里有一個內置的 json 庫,必須要學習好,json 是用于存儲和交換數據的語法,是一種輕量級的數據交換式使用場景,需要的朋友可以參考下
    2023-08-08
  • 使用 Python 破解壓縮文件的密碼的思路詳解

    使用 Python 破解壓縮文件的密碼的思路詳解

    這篇文章主要介紹了使用 Python 破解壓縮文件的密碼的思路詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Python多進程與服務器并發(fā)原理及用法實例分析

    Python多進程與服務器并發(fā)原理及用法實例分析

    這篇文章主要介紹了Python多進程與服務器并發(fā)原理及用法,深入淺出的介紹了進程、并行、并發(fā)、同步、異步等相關概念與原理,并結合實例形式給出了Python多進程編程相關操作技巧,需要的朋友可以參考下
    2018-08-08
  • Pycharm中的Python?Console用法解讀

    Pycharm中的Python?Console用法解讀

    這篇文章主要介紹了Pycharm中的Python?Console用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python 3.6打包成EXE可執(zhí)行程序的實現

    Python 3.6打包成EXE可執(zhí)行程序的實現

    這篇文章主要介紹了Python 3.6打包成EXE可執(zhí)行程序的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • Python PyQt5實現拖拽與剪貼板功能詳解

    Python PyQt5實現拖拽與剪貼板功能詳解

    這篇文章主要為大家詳細介紹了Python PyQt5如何實現拖拽與剪貼板功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-12-12

最新評論