這篇文章將為大家詳細(xì)講解有關(guān)Python中怎么使用裝飾器裝飾函數(shù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供鹽亭企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、H5開(kāi)發(fā)、小程序制作等業(yè)務(wù)。10年已為鹽亭眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
***個(gè)函數(shù)deco是裝飾函數(shù),它的參數(shù)就是被裝飾的函數(shù)對(duì)象。我們可以在deco函數(shù)內(nèi)對(duì)傳入的函數(shù)對(duì)象做一番“裝飾”,然后返回這個(gè)對(duì)象(記住一定要返回 ,不然外面調(diào)用foo的地方將會(huì)無(wú)函數(shù)可用。
我寫(xiě)了個(gè)小例子,檢查函數(shù)有沒(méi)有說(shuō)明文檔:、
static PyObject* thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) { PyObject *func, *args, *keyw = NULL; struct bootstate *boot; long ident; PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, &func, &args, &keyw); //[1]:創(chuàng)建bootstate結(jié)構(gòu) boot = PyMem_NEW(struct bootstate, 1); boot->interp = PyThreadState_GET()->interp; boot->funcfunc = func; boot->argsargs = args; boot->keywkeyw = keyw; //[2]:初始化多線程環(huán)境 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ //[3]:創(chuàng)建線程 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); return PyInt_FromLong(ident); [thread.c] /* Support for runtime thread stack size tuning. A value of 0 means using the platform's default stack size or the size specified by the THREAD_STACK_SIZE macro. */ static size_t _pythread_stacksize = 0; [thread_nt.h] long PyThread_start_new_thread(void (*func)(void *), void *arg) {
Python裝飾器是裝飾函數(shù),它的參數(shù)是用來(lái)加強(qiáng)“加強(qiáng)裝飾”的。由于此函數(shù)并非被裝飾的函數(shù)對(duì)象,所以在內(nèi)部必須至少創(chuàng)建一個(gè)接受被裝飾函數(shù)的函數(shù),然后返回這個(gè)對(duì)象(實(shí)際上此時(shí)foo=decomaker(arg)(foo))。
這個(gè)我還真想不出什么好例子,還是見(jiàn)識(shí)少啊,只好借用同步鎖的例子了:
def synchronized(lock): """鎖同步裝飾方法 !lock必須實(shí)現(xiàn)了acquire和release方法 """ def sync_with_lock(func): def new_func(*args, **kwargs): lock.acquire() try: return func(*args, **kwargs) finally: lock.release() new_func.func_name = func.func_name new_func.__doc__ = func.__doc__ return new_func return sync_with_lock @synchronized(__locker) def update(data): """更新計(jì)劃任務(wù)""" tasks = self.get_tasks() delete_task = None for task in tasks: if task[PLANTASK.ID] == data[PLANTASK.ID]: tasks.insert(tasks.index(task), data) tasks.remove(task) delete_task = task r, msg = self._refresh(tasks, delete_task) return r, msg, data[PLANTASK.ID]
調(diào)用時(shí)還是updae(data),同時(shí)還可以將多個(gè)裝飾器組合 使用:
def synchronized(lock): """鎖同步裝飾方法 !lock必須實(shí)現(xiàn)了acquire和release方法 """ def sync_with_lock(func): def new_func(*args, **kwargs): lock.acquire() try: return func(*args, **kwargs) finally: lock.release() new_func.func_name = func.func_name new_func.__doc__ = func.__doc__ return new_func return sync_with_lock @synchronized(__locker) def update(data): """更新計(jì)劃任務(wù)""" tasks = self.get_tasks() delete_task = None for task in tasks: if task[PLANTASK.ID] == data[PLANTASK.ID]: tasks.insert(tasks.index(task), data) tasks.remove(task) delete_task = task r, msg = self._refresh(tasks, delete_task) return r, msg, data[PLANTASK.ID]
關(guān)于Python中怎么使用裝飾器裝飾函數(shù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站欄目:Python中怎么使用裝飾器裝飾函數(shù)
文章路徑:http://chinadenli.net/article46/jhhheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、服務(wù)器托管、軟件開(kāi)發(fā)、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)