How to Use the Python modifier to decorate Functions

Source: Internet
Author: User
Tags python decorator

After a long time learning the Python annotator, I would like to share with you that you will certainly have a lot of gains after reading this article. I hope this article will teach you more things and learn about the Python annotator, you may encounter a Python modifier problem. Here we will introduce the solution to the Python modifier problem. Here we will share it with you.

The first deco function is a decoration function. Its parameter is the decorated function object. In the deco function, we can "Decorate" the input function object, and then return this object. Remember to return it. Otherwise, no function is available when foo is called outside.

I wrote a small example to check whether the function has any instructions :,

 
 
  1. Static PyObject * thread_PyThread_start_new_thread (PyObject * self, PyObject
  2.  
  3. * Fargs)
  4.  
  5. {
  6.  
  7. PyObject * func, * args ,*Keyw=NULL;
  8.  
  9. Struct bootstate * boot;
  10.  
  11. Long ident;
  12.  
  13. PyArg_UnpackTuple (fargs, "start_new_thread", 2, 3, & func, & args, & keyw );
  14.  
  15. // [1]: Create a bootstate Structure
  16.  
  17. Boot=PyMem_NEW(Struct bootstate, 1 );
  18.  
  19. Boot->Interp=PyThreadState_GET()->Interp;
  20.  
  21. Boot->FuncFunc= Func;
  22.  
  23. Boot->ArgsArgs= Args;
  24.  
  25. Boot->KeywKeyw= Keyw;
  26.  
  27. // [2]: Initialize the multi-threaded Environment
  28.  
  29. PyEval_InitThreads ();/* Start the interpreter's thread-awareness */
  30.  
  31. // [3]: Create a thread
  32.  
  33. Ident=PyThread_start_new_thread(T_bootstrap, (void *) boot );
  34.  
  35. Return PyInt_FromLong (ident );
  36.  
  37. [Thread. c]
  38.  
  39. /* Support for runtime thread stack size tuning.
  40.  
  41. A value of 0 means using the platform's default stack size
  42.  
  43. Or the size specified by the THREAD_STACK_SIZE macro .*/
  44.  
  45. Static size_t_ Pythread_stacksize=0;
  46.  
  47. [Thread_nt.h]
  48.  
  49. Long PyThread_start_new_thread (void (* func) (void *), void * arg)
  50.  
  51. {

The Python decorator is a decoration function, and its parameters are used to enhance "enhance decoration. Because this function is not a function object to be decorated, you must create at least one function internally to accept the function to be decorated, and then return this object. In fact, foo = decomaker (arg) (foo )).

I really don't have any good examples, but I still have little knowledge. I have to use the synchronization lock example:

 
 
  1. Def synchronized (lock ):
  2. "Lock synchronization decoration method
  3. ! The lock must implement the acquire and release methods.
  4. """
  5. Def sync_with_lock (func ):
  6. Def new_func (* args, ** kwargs ):
  7. Lock. acquire ()
  8. Try:
  9. Return func (* args, ** kwargs)
  10. Finally:
  11. Lock. release ()
  12. New_func.func_name= Func. func_name
  13. New_func. _ doc __= Func. _ doc __
  14. Return new_func
  15. Return sync_with_lock
  16. @ Synchronized (_ locker)
  17. Def update (data ):
  18. "Update scheduled task """
  19. Tasks=Self. Get_tasks ()
  20. Delete_task=None
  21. For task in tasks:
  22. If task [PLANTASK. ID] = data [PLANTASK. ID]:
  23. Tasks. insert (tasks. index (task), data)
  24. Tasks. remove (task)
  25. Delete_task= Task
  26. R,Msg=Self. _ Refresh (tasks, delete_task)
  27. Return r, msg, data [PLANTASK. ID]

The call is still performed by updae (data). You can also combine multiple decorators:

 
 
  1. Def synchronized (lock ):
  2. "Lock synchronization decoration method
  3. ! The lock must implement the acquire and release methods.
  4. """
  5. Def sync_with_lock (func ):
  6. Def new_func (* args, ** kwargs ):
  7. Lock. acquire ()
  8. Try:
  9. Return func (* args, ** kwargs)
  10. Finally:
  11. Lock. release ()
  12. New_func.func_name= Func. func_name
  13. New_func. _ doc __= Func. _ doc __
  14. Return new_func
  15. Return sync_with_lock
  16. @ Synchronized (_ locker)
  17. Def update (data ):
  18. "Update scheduled task """
  19. Tasks=Self. Get_tasks ()
  20. Delete_task=None
  21. For task in tasks:
  22. If task [PLANTASK. ID] = data [PLANTASK. ID]:
  23. Tasks. insert (tasks. index (task), data)
  24. Tasks. remove (task)
  25. Delete_task= Task
  26. R,Msg=Self. _ Refresh (tasks, delete_task)
  27. Return r, msg, data [PLANTASK. ID]

After learning, I always feel that the Python modifier can make the function light, and more importantly, place the constraints of the function at the interface to make the intent clearer without increasing the caller's burden, this post is still superficial. I will definitely be back!

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and encoding solution
  5. Analysis of Python GIL and thread security

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.