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 :,
- 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]: Create a bootstate Structure
-
- Boot=PyMem_NEW(Struct bootstate, 1 );
-
- Boot->Interp=PyThreadState_GET()->Interp;
-
- Boot->FuncFunc= Func;
-
- Boot->ArgsArgs= Args;
-
- Boot->KeywKeyw= Keyw;
-
- // [2]: Initialize the multi-threaded Environment
-
- PyEval_InitThreads ();/* Start the interpreter's thread-awareness */
-
- // [3]: Create a thread
-
- 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)
-
- {
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:
- Def synchronized (lock ):
- "Lock synchronization decoration method
- ! The lock must implement the acquire and release methods.
- """
- 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 ):
- "Update scheduled task """
- 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]
The call is still performed by updae (data). You can also combine multiple decorators:
- Def synchronized (lock ):
- "Lock synchronization decoration method
- ! The lock must implement the acquire and release methods.
- """
- 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 ):
- "Update scheduled task """
- 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]
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!
- How to embed Python into C ++ applications?
- In-depth discussion of Ruby and Python syntax comparison
- Introduction to Python
- Python Learning Experience: version, IDE selection and encoding solution
- Analysis of Python GIL and thread security