Python中怎么使用装饰器装饰函数

发布时间:2021-07-20 13:43:56 作者:Leah
来源:亿速云 阅读:162

这篇文章将为大家详细讲解有关Python中怎么使用装饰器装饰函数,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

***个函数deco是装饰函数,它的参数就是被装饰的函数对象。我们可以在deco函数内对传入的函数对象做一番“装饰”,然后返回这个对象(记住一定要返回 ,不然外面调用foo的地方将会无函数可用。

我写了个小例子,检查函数有没有说明文档:、

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]:创建bootstate结构       boot = PyMem_NEW(struct bootstate, 1);       boot->interp = PyThreadState_GET()->interp;       boot->funcfunc = func;       boot->argsargs = args;       boot->keywkeyw = keyw;       //[2]:初始化多线程环境       PyEval_InitThreads(); /* Start the interpreter's thread-awareness */       //[3]:创建线程       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装饰器是装饰函数,它的参数是用来加强“加强装饰”的。由于此函数并非被装饰的函数对象,所以在内部必须至少创建一个接受被装饰函数的函数,然后返回这个对象(实际上此时foo=decomaker(arg)(foo))。

这个我还真想不出什么好例子,还是见识少啊,只好借用同步锁的例子了:

def synchronized(lock):         """锁同步装饰方法        !lock必须实现了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):     """更新计划任务"""        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]

调用时还是updae(data),同时还可以将多个装饰器组合 使用:

def synchronized(lock):         """锁同步装饰方法        !lock必须实现了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):     """更新计划任务"""        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]

关于Python中怎么使用装饰器装饰函数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Python装饰器函数
  2. 怎么使用Python装饰器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:mysql服务器端的组件有什么用

下一篇:怎么修改gazebo物理参数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》