In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what looks like high-end technology in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Metaprogramming
To put it simply, metaprogramming is a language that can manipulate the target language. For example, I have a class, and I want to get the information about the class when I generate the class, then this process is metaprogramming.
If you have ever understood reflection, you will find that this is very similar to reflection, yes, reflection is a way to achieve metaprogramming.
In python, metaprogramming is generally implemented through magic methods, descriptors, metaclasses, and eval.
GIL
The full name of GIL is global interpreter lock, which is called global interpreter lock.
We all know that the smallest unit of operating system scheduling is threads, and there can be multiple threads in a process. When multiple threads run concurrently, they may modify the same piece of memory, resulting in data inconsistencies. In order to solve this problem, the cpython interpreter will let each thread get the GIL. Only the thread that successfully obtains the GIL can execute, and other threads need to wait. This is why thread concurrency in python is pseudo-concurrency.
So how to make better use of multi-core cpu, the easiest way is to use multi-processes, because GIL is unique in threads, and processes do not need to get it, so you can run multiple processes to achieve concurrent execution of the program.
Cooperative process
We know that the smallest unit of operating system scheduling is threads, so what is the co-program? Generally speaking, a co-program is a user-mode thread, that is to say, we encapsulate a thread in which we can schedule ourselves, and when performing time-consuming operations, we give up the tasks we execute to perform other tasks, that is, let cpu see that our thread has been executing the task without waiting.
In python, the cooperative program is scheduled by the yield keyword, the task state can be saved through the yield, and the function result can be passed to another function through the send keyword.
Context Manager
We usually operate files or databases, when we use them, we need to close them manually, so is there any way to automatically release resources? The answer is yes, we can use the with keyword.
With open ('test.txt') as f: print f.readlines ()
By doing so, we automatically release file resources after the function is executed.
How is the context manager implemented? its principle is that its underlying implementation of the _ _ enter__ and _ _ exit__ methods, so that before the execution of the function body, we can first execute the enter method, after the main body execution, we then execute the exit method.
Through the contextlib decorator in python, the context manager can be implemented gracefully.
Decorator
As the name implies, the decorator is a decoration to the function, which decorates the function to make it look different without modifying the main content of the function.
In python, functions can be easily decorated by adding the @ symbol in front of them, and many logging functions and authorization functions become more elegant through the use of decorators.
Closure
Closure is a concept in many languages. In python, closure is the nesting of functions, but this is not a simple nesting of functions, it needs to meet several conditions.
The return value of the external function is a reference to the internal function
Nonlcoal modifies external parameters
Python can implement the decorator more conveniently through the use of closures.
This is the end of the content of "what looks like high-end technologies in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.